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数据
相关推荐
浩泽学编程2 分钟前
内网开发?系统环境变量无权限配置?快速解决使用其他版本node.js
前端·vue.js·vscode·node.js·js
海南java第二人3 分钟前
打破Java双亲委派模型的三大核心场景与技术实现
java·spring
天天摸鱼的java工程师3 分钟前
分布式 ID 生成终极方案:雪花算法优化与高可用实现
java·后端
狗哥哥4 分钟前
Vue 3 插件系统重构实战:从过度设计到精简高效
前端·vue.js·架构
巾帼前端4 分钟前
前端对用户因果链的优化
前端·状态模式
沛沛老爹5 分钟前
2025年java总结:缝缝补补又一年?
java·开发语言·人工智能·python·guava·总结·web转型ai
艾迪的技术之路6 分钟前
【实践】2025年线上问题解决与总结-3
java
雨中飘荡的记忆6 分钟前
MyBatis参数处理模块详解
java·mybatis
Chloeis Syntax8 分钟前
MySQL初阶学习日记(7)--- 事务
java·数据库·笔记·学习·mysql
不想秃头的程序员8 分钟前
Vue3 中 Lottie 动画库的使用指南
前端