码迷,mamicode.com
首页 > 其他好文 > 详细

接口调用工具类

时间:2019-10-21 11:58:34      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:http   cat   custom   type   connect   post   encode   build   data   

public class HttpClientApi {

 

public static JSONObject getData(String url,String data) {

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
httpPost.setEntity(new StringEntity(data));
HttpResponse response = httpClient.execute(httpPost);
JSONObject jsonObject = JSONObject.parseObject(EntityUtils
.toString(response.getEntity()));
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

 

//传String 接收JSONArray
public static JSONArray getData2(String url,String data) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
httpPost.setEntity(new StringEntity(data));
HttpResponse response = httpClient.execute(httpPost);
JSONArray JSONArrays = JSONArray.parseArray(EntityUtils
.toString(response.getEntity()));
return JSONArrays;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

 

public static String getData3(String url, JSONObject jsonObject) {
//url = Config.get("ac-product-api.url") + url;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(180 * 1000)
.setConnectionRequestTimeout(180 * 1000)
.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/json");
try {
httpPost.setEntity(new StringEntity(jsonObject.toString(),
ContentType.create("application/json", "utf-8")));
HttpResponse response = httpClient.execute(httpPost);
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
return "post failure :caused by-->" + e.getMessage().toString();
}
}

 

//传json 接收JSONArray
public static JSONArray getData4(String url, JSONObject jsonObject) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(180 * 1000)
.setConnectionRequestTimeout(180 * 1000)
.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/json");
try {
httpPost.setEntity(new StringEntity(jsonObject.toString(),
"UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
JSONArray JSONArrays = JSONArray.parseArray(EntityUtils
.toString(response.getEntity(),"UTF-8"));
return JSONArrays;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

 

 

 

}

接口调用工具类

标签:http   cat   custom   type   connect   post   encode   build   data   

原文地址:https://www.cnblogs.com/lifan12589/p/11712092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!