我在查找有关向本地服务器发送 JSON 请求的最新信息时遇到了麻烦。我不断遇到使用不推荐使用的代码的示例,我真的很想用不推荐使用的代码来做到这一点。
我至少可以说我现在有一个工作示例,并且我没有从 NetBeans 收到任何已弃用的消息,但我想知道我整理的方法是否正确:
public void sendUpdateRequest() {
String updateString =
"{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\"}" ;
StringEntity entity = new StringEntity(updateString, Consts.UTF_8);
HttpPost httpPost = new HttpPost(getURL()); // http://xbmc:xbmc@10.0.0.151:8080/jsonrpc
entity.setContentType("application/json");
httpPost.setEntity(entity);
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
HttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine()); // move to log
}
catch (IOException e) {
e.printStackTrace(); // move to log
}
}
这是我正在努力使用 JSON HTTP 请求更新 XBMC
编辑
更改了代码以根据评论尝试使用资源——希望这对处理 JSON 和 Java 的其他人有用