1
我有一個時間查找有關發送JSON請求本地服務器的最新信息。我不斷遇到使用不推薦使用的代碼的示例,並且我非常希望使用不是的代碼來執行此操作。有沒有人有發佈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:[email protected]: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
並且close()應該在finally塊中調用;) – Muel
@Muel謝謝!更新。 –
所以我最初是這樣做的,但是然後close()方法想要拋出另一個'IOException',所以我會在finally塊中做另一個try/catch - 是不好的編碼? –