public static JSONObject doGetStr(String url) { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); JSONObject jsonObject = null; try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.fromObject(response); } } catch (IOException e) { e.printStackTrace(); } return jsonObject; }
我这么写得到的entity根本不是想要的
第11行:
jsonObject = JSONObject.fromObject(result); // 这里照着IDEA的提示按快了
另外,第7行替换为:
CloseableHttpResponse response = httpClient.execute(httpGet);
这个是官网示例的用法。小伙伴们如果遇到deprecated可以使用CloseableHttpClient替换。