编辑:感谢您的建议。我更改了代码以实现 json 对象而不是字符串并在正文中发送
这就是我试图从调查猴子那里得到的细节。我得到的响应代码为 200,但无法获得我需要的数据。有人可以让我知道我在哪里做错了。可能这个 url 可以帮助指定我正在尝试做的事情 https://developer.surveymonkey.com/mashery/requests_responses 这是 o/p 我得到 {"status": 3, "errmsg":"No JSON对象可以被解码:第 1 行第 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();
}
}
}