OkHttp上传Json嵌套对象
?
/*** 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嵌套对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA 底部工具栏没有 Version
- 下一篇: win10硬盘修复工具使用教程