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数据
相关推荐
普郎特4 分钟前
"不再迷惑!用'血缘关系'彻底搞懂JavaScript原型链机制"
前端·javascript
可观测性用观测云14 分钟前
前端错误可观测最佳实践
前端
恋猫de小郭14 分钟前
Android 将强制应用使用主题图标,你怎么看?
android·前端·flutter
道可到18 分钟前
Java 反射现代实践速查表(JDK 11+/17+)
java
道可到29 分钟前
Java 反射现代实践指南(JDK 11+ / 17+ 适用)
java
一枚前端小能手35 分钟前
「周更第3期」实用JS库推荐:Lodash
前端·javascript
艾小码35 分钟前
Vue组件到底怎么定义?全局注册和局部注册,我踩过的坑你别再踩了!
前端·javascript·vue.js
玉衡子38 分钟前
九、MySQL配置参数优化总结
java·mysql
叽哥39 分钟前
Kotlin学习第 8 课:Kotlin 进阶特性:简化代码与提升效率
android·java·kotlin
麦兜*41 分钟前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud