zoukankan      html  css  js  c++  java
  • 接口调用工具类

    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;
    }
    }

    }

  • 相关阅读:
    数据库注意事项
    SQL函数
    2019 SDN阅读作业(2)
    2019 SDN上机第五次作业
    2019 SDN上机第四次作业
    2019 SDN阅读作业
    2019 SDN上机第三次作业
    mysql使用记录
    2019 SDN上机第二次作业
    2019 SDN上机第一次作业
  • 原文地址:https://www.cnblogs.com/lifan12589/p/11712092.html
Copyright © 2011-2022 走看看