public void doGet(String text) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String ip = environment.getProperty("ip");
String port = environment.getProperty("port");
String url = "http://" + ip + ":" + port + "/search?text=" + text;
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("内容"+EntityUtils.toString(entity));
String res = EntityUtils.toString(entity);
JSONObject jsonResult = new JSONObject(res);
JSONArray jsonArr = jsonResult.getJSONArray("rows");
for (int i = 0; i < jsonArr.size(); i++) {
JSONObject jObject = jsonArr.getJSONObject(i);
//
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
try {
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
springboot发起get请求
最新推荐文章于 2024-10-24 11:36:33 发布
本文介绍了一个使用Java HttpClient进行网络请求的具体实现案例。该案例通过构建HTTP GET请求来发送搜索查询,并解析返回的JSON结果,展示了如何获取并打印出搜索结果中的特定数据。
7791

被折叠的 条评论
为什么被折叠?



