【发布时间】:2018-10-29 11:31:18
【问题描述】:
我正在尝试调用一个 REST 服务,该服务接受带有两个参数 param1 和 param2 的 json,如下所示:
[{
"param1": "xxx",
"param2": "xxx"
}]
按照我的代码调用 MY_SERVICE_URL:
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(MY_SERVICE_URL);
String json = new String("[{\"param1\": \"" + "param1Value" + "\",\"param2\": \"" + "param2Value" + "\"}]");
request.setEntity(new StringEntity(json));
request.setHeader("Content-Type", "application/json");
request.setHeader("Accept", "application/json");
try {
HttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
String output = rd.readLine();
System.out.println("Response code: " + responseCode);
System.out.println("Output: " + output);
} catch(Exception ex) {
System.out.println("Error");
}
client.getConnectionManager().shutdown();
POST 失败,因为它没有将第一个参数识别为有效:而不是读取 param1,它正在考虑参数 \"param1\"
任何帮助表示赞赏 谢谢 西蒙娜
【问题讨论】:
-
您好 Simone,请您在您的问题中添加堆栈跟踪的 sn-p 吗?这对于了解正在发生的事情以及为遇到相同问题的人检索您的问题通常很有用:)
标签: java json post http-post httpclient