/**
*ip 访问地址
*return jsonObject
*/
public JSONObject jsonObject(String ip) throws IOException {
String ip =ip;
InputStream is = null;
String body = null;
StringBuilder res=new StringBuilder();
HttpPost httpPost = new HttpPost(ip);
httpPost.addHeader("Content-Type", "application/json");
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).build();
httpPost.setConfig(config);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity != null){
is = entity.getContent();
//转换为字节输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
while((body=br.readLine()) != null){
res.append(body);
}
}
JSONObject jsonObject = JSONObject.parseObject(String.valueOf(res));
//拿到json中的值
String xxxx = jsonObject.getString("键");
return jsonObject;
}
后端进行接口访问返回json字符串
于 2022-08-01 10:17:36 首次发布