0

当我在 zoho 书籍中调用此列表项 API 时:

https://www.zoho.com/invoice/api/v3/settings/items/#list-items,响应为:{"code":4,"message":"为 JSONString 传递的值无效"}

API 是如何工作的

HTTP 动词用于访问资源 - GET、POST、PUT 和 DELETE。请求中的所有参数都应该是 form-urlencoded。对于您需要传递 authtoken 和 organization_id 的所有 API。输入 JSON 字符串应使用 JSONString 参数传递。

    private void testZoho() {
    JSONObject JSONString = new JSONObject();

    JSONString.put("sort_column", "name");

    String urlParameters ="authtoken=xxxxxx&organization_id=xxxxx";
    urlParameters += "&JSONString=" + JSONString.toJSONString();
    System.out.println("retJson1:\n" + urlParameters);
    HttpURLConnection httpcon;  
    String url = "https://books.zoho.com/api/v3/items";
    String data = urlParameters;
    String result = null;
    try{
    //Connect
    httpcon = (HttpURLConnection) ((new URL (url).openConnection()));
    httpcon.setDoOutput(true);

    String charset = "UTF-8";
    httpcon.setRequestProperty("Accept-Charset", charset);
    httpcon.setRequestProperty("Content-Type", "application/json;charset=" + charset);
    httpcon.setRequestMethod("GET");
    httpcon.connect();

    //Write         
    OutputStream os = httpcon.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
    writer.write(data);
    writer.close();
    os.close();

    //Read      
    BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(),"UTF-8"));

    String line = null; 
    StringBuilder sb = new StringBuilder();         

    while ((line = br.readLine()) != null) {  
         sb.append(line); 
    }       

    br.close();  
    result = sb.toString();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 
    System.out.println("retJson1:\n" + result);

}
4

1 回答 1

1

您需要将所有参数作为 url 参数传递,而不是写入流中。我修改了你的代码:

private void testZoho()
{
    HttpURLConnection httpcon;
    String url = "https://books.zoho.com/api/v3/items?authtoken=xxxx&organization_id=xxx&sort_column=name";
    String result = null;
    try
    {
        //Connect
        httpcon = (HttpURLConnection) ((new URL(url).openConnection()));
        httpcon.setDoOutput(true);

        String charset = "UTF-8";
        httpcon.setRequestProperty("Accept-Charset", charset);
        httpcon.setRequestProperty("Content-Type", "application/json;charset=" + charset);
        httpcon.setRequestMethod("GET");
        httpcon.connect();

        //Read
        BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(), "UTF-8"));

        String line = null;
        StringBuilder sb = new StringBuilder();

        while((line = br.readLine()) != null)
        {
            sb.append(line);
        }

        br.close();
        result = sb.toString();

    }
    catch(UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    System.out.println("retJson1:\n" + result);
}
于 2015-08-29T06:47:34.017 回答