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数据
相关推荐
GIS之路1 分钟前
ArcGIS Pro 中的 Python 入门
前端
树獭非懒10 分钟前
告别繁琐多端开发:DivKit 带你玩转 Server-Driven UI!
android·前端·人工智能
兆子龙42 分钟前
当「多应用共享组件」成了刚需:我们从需求到模块联邦的落地小史
前端·架构
Qinana43 分钟前
从代码到智能体:MCP 协议如何重塑 AI Agent 的边界
前端·javascript·mcp
Wect1 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
不会敲代码11 小时前
从入门到进阶:手写React自定义Hooks,让你的组件更简洁
前端·react.js
用户5433081441941 小时前
拆完 Upwork 前端我沉默了:你天天卷的那些技术,人家根本没用
前端
洋洋技术笔记1 小时前
Vue实例与数据绑定
前端·vue.js
Re_zero1 小时前
线上日志被清空?这段仅10行的 IO 代码里竟然藏着3个毒瘤
java·后端
Marshall1511 小时前
zzy-scroll-timer:一个跨框架的滚动定时器插件
前端·javascript