【问题标题】:surveymonkey API call return status code 3- Invalid requestsurveymonkey API 调用返回状态码 3- 无效请求
【发布时间】:2014-03-07 21:13:19
【问题描述】:

编辑:感谢您的建议。我更改了代码以实现 json 对象而不是字符串并在正文中发送

这是我试图从调查猴子那里得到的详细信息。我得到的响应代码为 200,但无法获得我需要的数据。有人可以让我知道我在哪里做错了。 这个网址可能有助于指定我正在尝试做的事情 https://developer.surveymonkey.com/mashery/requests_responses 这是 o/p 我得到 {"status": 3, "errmsg": "No JSON object could be decoded: line 1 column 0 (char 0)"}

    package surveydetails;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Surveydetails {

        public static void main(String args []) {       
        String url = "https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=myapikey";                    
                    System.out.println("request being sent");
                    System.out.println(url);
                    JSONObject obj = new JSONObject();



                    try {
                        //byte[] postDataBytes = obj.toJSONString().getBytes("UTF-8");
                        URL ourl = new URL(url.toString());
                        HttpURLConnection conn = (HttpURLConnection) ourl.openConnection();
                        conn.setRequestMethod("POST");


conn.setRequestProperty("Authorization", "bearer myauthtoken"); 
conn.setRequestProperty("Content-Type", "application/json");

conn.getRequestProperty(obj.toString().getBytes("UTF-8").toString());


                        int k = conn.getResponseCode();
                        System.out.println("The response code received is "+k);
                        if (conn.getResponseCode() != 200) {
                            throw new RuntimeException("Failed : HTTP error code : "
                                    + conn.getResponseCode());
                        }
                        BufferedReader br = new BufferedReader(new InputStreamReader(
                                (conn.getInputStream())));

                        String output;

                        System.out.println("Output from Server .... \n");


                            output = br.readLine();
                            System.out.println(output);


                    } catch (MalformedURLException e) {

                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
}

【问题讨论】:

  • 试着去掉你身体里的单引号。
  • 是的,我确实尝试过这样做。响应仍然相同:{"status": 3, "errmsg": "No JSON object could be decoded: line 1 column 0 (char 0)"}

标签: java client httpurlconnection surveymonkey


【解决方案1】:

POST 正文的 JSON 编码无效。

你的代码String body=" '{\"fields\":[\"title\"]}'";

应该是String body="{\"fields\":[\"title\"]}";

我建议在编码 JSON 对象时使用像 json-simple 这样的 JSON 库以避免无效的 JSON。随着应用程序的开发,库也将更加灵活。

【讨论】:

  • 下面是工作代码。感谢您提出更改建议。唯一的额外变化是再次设置我的身体。 String input ="{}"; OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
相关资源
最近更新 更多