【发布时间】:2013-06-17 16:19:37
【问题描述】:
我正在尝试编写一个使用 HTTP GET 方法的 JSON 客户端,我得到的响应为 500。我尝试过的代码如下,有什么问题吗?我尝试设置的请求参数是 entityName=Nila 和 parentEntity=500000001 作为参数。
URL url = new URL("http://192.168.210.74:9763/services/testEntityService?entityName=Nila&parentEntity=500000001");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
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");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
嗯,问题出在服务器端……
-
首要任务 - 您要连接的服务是否接受应用程序/json?我建议问题出在服务器端,而不是客户端
标签: java json rest restful-url