日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OkHttp上传Json嵌套对象

發布時間:2023/12/3 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OkHttp上传Json嵌套对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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));
————————————————
版權聲明:本文為CSDN博主「「已注銷」」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/easy_purple/article/details/90283501

總結

以上是生活随笔為你收集整理的OkHttp上传Json嵌套对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。