OkHttp上传Json嵌套对象

    技术2023-06-16  67

    public static DevInfoVo queryRCP() throws Exception { // 东八区时区 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00")); //JSONObject obj = JSON.parseObject(JSON.toJSONString(pojo)); JSONObject obj = new JSONObject(); obj.put("Authorization", "accessKey=WR3512xPe7UAiFCxoKez5KPu&path=%2FaddDevice&timestamp=1590459327607&method=SHA1&sign=7b4818f59f53e3b529c76c560a7d65014e1f7388"); FormBody.Builder body = new FormBody.Builder(); for (Map.Entry<String, Object> entry : obj.entrySet()) { if (entry.getValue() != null) { body.addEncoded(entry.getKey(), entry.getValue().toString()); } } Request.Builder request = new Request.Builder() .url(sanAPI) .addHeader("Authorization", IotTokenUtil.getAuthorization()); //.post(body.build()); byte[] data = HttpUtil.executeBody(request); DevInfoVo devInfoVo = (DevInfoVo) JSON.parseObject(data, DevInfoVo.class); return devInfoVo; }

     

    /** * IOT接口查询 * * @param * @return * @throws IOException */ public static CmdResultVo queryRCP(SendOrderDto sendOrderDto) throws Exception { // 东八区时区 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00")); JSONObject body = new JSONObject(); JSONObject requestData = new JSONObject(); try { body.put("power", sendOrderDto.getParams().getPower()); requestData.put("params", body); requestData.put("cmd", sendOrderDto.getCmd()); } catch (JSONException e) { e.printStackTrace(); } RequestBody requestBody = FormBody .create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData)); Request.Builder request = new Request.Builder() .url(sanAPI) .addHeader("Authorization", IotTokenUtil.getAuthorization()) .post(requestBody); byte[] data = HttpUtil.executeBody(request); CmdResultVo cmdResultVo = (CmdResultVo) JSON.parseObject(data, CmdResultVo.class); return cmdResultVo; }

     

     

     

     

    public class HttpUtil { private static OkHttpClient client = buildClient(5); public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json;charset=UTF-8"); /** * 执行器 请求失败将直接抛出异常 * * @param request * @return 返回 data 节点数据 * @throws IOException */ public static JSONObject execute(Request.Builder request) throws IOException { Call call = client.newCall(request .build()); return execute(call); } /** * 执行器 * * @param call * @return * @throws IOException */ public static JSONObject execute(Call call) throws IOException { try (Response res = call.execute()) { if (!res.isSuccessful()) { throw new RuntimeException("peake.api request error " + res.code() + " " + res.message() + " " + call.request().url().toString()); } if (res.body() == null) { throw new RuntimeException("peake.api response body must not null"); } String body = new String(res.body().bytes()); return JSON.parseObject(body); } } /** * 执行器 * * @param request * @return * @throws IOException */ public static byte[] executeBody(Request.Builder request) throws IOException { Call call = client.newCall(request .build()); return executeBody(call); } /** * 执行器 * * @param client * @param request * @return * @throws IOException */ public static byte[] executeBody(OkHttpClient client, Request request) throws IOException { Call call = client.newCall(request); return executeBody(call); } /** * 执行器 * * @param call * @return * @throws IOException */ public static byte[] executeBody(Call call) throws IOException { try (Response res = call.execute()) { if (!res.isSuccessful()) { throw new RuntimeException(MessageFormat.format("request error {0} {1} {2}", res.code(), res.message(), call.request().url())); } if (res.body() == null) { return null; } return res.body().bytes(); } } public static OkHttpClient buildClient(long seconds) { return new OkHttpClient.Builder() .callTimeout(Duration.ofSeconds(seconds)) .build(); } public static OkHttpClient buildClient(long seconds, boolean redirect) { return new OkHttpClient.Builder() .callTimeout(Duration.ofSeconds(seconds)) .followRedirects(redirect) .followSslRedirects(redirect) .build(); } }

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    OkHttp上传Json嵌套对象

    「已注销」 2019-05-17 01:38:28  440  收藏 分类专栏: Android 版权 应需求,需要传递一个如下的表单,使用传统的formbody提交键值对是不太好实现的。

    {   "properties": {       "name": "Imfondof",       "age": 17,           "sex": "不详"   } }   所以用到了Json:

    将内部的属性封装到一个Json对象里, 然后将这个Json对象作为主Json对象的值传入 将主Json转化为RequestBody 传入到我们的请求体中即可 JSONObject body = new JSONObject(); JSONObject requestData = new JSONObject(); try {     body.put("name", name);     body.put("age", age);     body.put("sex", sex);     requestData.put("otherProps", body); } catch (JSONException e) {     e.printStackTrace(); } RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData)); ———————————————— 版权声明:本文为博主「「已注销」」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/easy_purple/article/details/90283501

    Processed: 0.012, SQL: 9