String url =“第三方调用api的完整路径”;
CloseableHttpClient build = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(url);
(请求方式可以更换,HttPost)
CloseableHttpResponse response = null;
try {
response = build.execute(get);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
InputStream instreams = httpEntity.getContent();
String str = ConvertStreamToString(instreams);
Map<?, ?> map = JSONUtil.jsonToMap(str);
(这里是将string转为map)
}
} catch (IOException e) {
e.printStackTrace();
}
//JSONUtil.jsonToMap方法
public static Map<?, ?> jsonToMap(String jsonStr) {
Map<?, ?> objMap = null;
if (!StringUtil.isEmpty(jsonStr)) {
objMap = JSON.parseObject(jsonStr, Map.class);
}
return objMap;
}
以上就是http调用接口的步骤,是我业务需求的开发的步骤,如有不同的,请多多指教,共同探讨