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

    }

  • 相关阅读:
    char/byte/short类型的加法和类型转换问题
    Java四种基本数据类型
    Git知识集锦
    解决给自己的博客添加百度统计不能验证的问题
    C++静态代码分析工具推荐——PVS-Studio
    Qt在控件未显示时如何获取正确的控件尺寸
    C#程序如何捕捉未try/catch的异常——不弹“XXX已停止工作”报错框
    win10下vs2015编译的程序如何运行在win7等系统(无需安装Redistributable)
    Qt分页导航控件
    win server 2008配置ftp无法登陆问题的解决办法
  • 原文地址:https://www.cnblogs.com/lifan12589/p/11712092.html
Copyright © 2011-2022 走看看