java发送https请求以及解析调用接口返回来的数据信息

一、返回的Json数据信息格式

json 复制代码
{
"code":200,
"msg":"查询成功",
"data":[{
"name":"张三"
},{
"name":"李四"
}]
}

二、解析

引用的是

java 复制代码
import cn.hutool.json.JSONObject;

判断如果code为200则证明数据返回成功,然后获取data中的value数据。

java 复制代码
  // 解析JSON字符串  
        JSONObject jsonObject = new JSONObject(jsonString);  

获取data数据

java 复制代码
      // 获取data数组  
        JSONArray dataArray = jsonObject.getJSONArray("data");  

这样就可以获取data中的数据了。

如果想要将data中的数据变成集合

java 复制代码
JSONArray jsonArray = new JSONArray(data-value);
entityList = jsonArray.toList(实体.class);

就可以啦

三、发送https请求

java 复制代码
		String url = "";
        JSONObject json = new JSONObject();
        json.put("name","张三");
        String jsonString = json.toString();
        // httpclient
        SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new TrustStrategy() {
            @Override
            public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws CertificateException {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
                new String[]{"TLSv1.2", "TLSv1.1", "SSLv3"}, null, new NoopHostnameVerifier());
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        HttpPost httpPost = new HttpPost(url);
        // 超时时间设置成5s
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
        httpPost.setConfig(requestConfig);
        StringEntity entity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity);
        // Create a custom response handler
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
                log.info("response"+response);
                int status = response.getStatusLine().getStatusCode();
                log.info("status"+status);
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    String s = EntityUtils.toString(entity);
                    return s;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };
        String execute = httpclient.execute(httpPost, responseHandler);
        log.info("--------------------------------------111111111111111111111--------------------");
        log.info("responseBody------------>"+execute);
        return execute;
复制代码
 JSONObject json = new JSONObject(); 
 可以组装成Json数据
相关推荐
五岳22 分钟前
DTS按业务场景批量迁移阿里云MySQL表实战(下):迁移管理平台设计与实现
java·应用·dts
zhougl99638 分钟前
Java 所有关键字及规范分类
java·开发语言
Python 老手44 分钟前
Python while 循环 极简核心讲解
java·python·算法
java1234_小锋1 小时前
Java高频面试题:MyISAM索引与InnoDB索引的区别?
java·开发语言
2501_944525541 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
Mr_Xuhhh1 小时前
MySQL函数详解:日期、字符串、数学及其他常用函数
java·数据库·sql
李白你好2 小时前
Burp Suite插件用于自动检测Web应用程序中的未授权访问漏洞
前端
测试开发Kevin2 小时前
小tip:换行符CRLF 和 LF 的区别以及二者在实际项目中的影响
java·开发语言·python
笨手笨脚の2 小时前
Redis: Thread limit exceeded replacing blocked worker
java·redis·forkjoin·thread limit
Lenyiin2 小时前
Linux 基础IO
java·linux·服务器