2013-03-25 159 views
0

我有以下代码以asana创建任务。响应给出了错误

String data = "{" + " \"data\": {" 
     + "\"workspace\":4661999437107," 
     + "\"name\": \"crmit solutions\"," 
     + "\"notes\": \"NOTES_ABOUT_TASK_GO_HERE\"," 
     + "\"assignee\": \"[email protected]\"" + "}" 
     // + " \"options\": {" + "  \"fields\": \"name\"}" 
       + "}"; 

     HttpClient client = new HttpClient(); 
     client.getParams().setAuthenticationPreemptive(true); 
     client.getParams().setParameter("http.useragent", "Test Client"); 
     client.getState().setCredentials(
       new AuthScope("app.asana.com", 443, "realm"), 
       new UsernamePasswordCredentials(
         "1k3qqT7O.RVRyxHdHRLiYSH710c6NpVl", "")); 

     BufferedReader br = null; 
     PostMethod method = new PostMethod(
       "https://app.asana.com/api/1.0/tasks"); 
     method.setDoAuthentication(true); 

     try { 
      method.setRequestBody(data); 
      int returnCode = client.executeMethod(method); 
      System.out.println("Return code: " + returnCode); 
      br = new BufferedReader(new InputStreamReader(
        method.getResponseBodyAsStream())); 
      String readLine; 
      while (((readLine = br.readLine()) != null)) { 
       System.out.println(readLine); 
      } 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } finally { 
      method.releaseConnection(); 
      if (br != null) 
       try { 
        br.close(); 
       } catch (Exception fe) { 
        System.err.println(fe); 
       } 
     } 

,但它给我一个错误说返回代码:400 { “错误”:[{ “消息”: “工作区:类型不正确”}]}

请帮助!

回答

0

这意味着workspace通过的值为不正确类型。交叉检查它接受的值的类型。

如果它的字符串,请求json需要围绕工作空间的值有双引号(“”)

如果它是一个数组,那么您的请求需要被相应地构造(围绕值列表的方括号[])。

只要确保值的类型是同步的。

+0

您的建议很好,但在这种情况下,它不会真正解决他的问题。我们应该改进错误信息,使其更清楚地表明它所指的对象是错误的类型,而不是JSON值。 :) – 2013-03-26 16:43:36

0

(I在体位工作。)

此错误消息表示的ID你传入,4661999437107,是指对象不是一个工作区。使用/workspaces端点来获取您所在工作区的列表。