我用Java编写代码。 API接受以下格式的输入:{input:“[123,345]”}
我的HTTP Post请求代码如下:
输入参数:urlStr是我要连接的API的URL
和paramVal是JSONObject.toString格式。
我在创建paramVal值的代码:
int[] param_value = {123};
JSONObject obj=new JSONObject();
obj.put("job_ids", param_value);
String response = httpPost(url,obj.toString)
在调试器中检查时,paramVal的httpPost值为 { “输入”:[123]}
public static String httpPost(String urlStr, String paramVal) throws Exception {
URL url = new URL(urlStr);
HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Create the form content
if (paramVal != null){
OutputStream out = conn.getOutputStream();
Writer writer = new OutputStreamWriter(out);
writer.write(paramVal);
writer.close();
out.close();
}
if (conn.getResponseCode() != 200) {
throw new IOException(conn.getResponseMessage());
}
// Buffer the result into a string
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
return sb.toString();
}
此HTTP后呼叫收到错误请求错误。 我是RESTful API的新手,并且是第一次使用它。请帮我在代码中找到错误