您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
安卓okhttp发送json与解析json(支持嵌套)
发布时间:2022-09-25 23:34:26编辑:雪饮阅读()
代码片段如:
Log.d("xy_step5_347", "xxxx");
JSONObject requestJson = new JSONObject();
Log.d("xy_step5_364", "");
requestJson.put("outTradeNo",outTradeNo);
//支付金额 元
//requestJson.put("totalAmount",IFSMainActivity.totalAmount);
requestJson.put("totalAmount",totalAmount);
Log.d("xy_step5_369", "");
requestJson.put("authCode",open_face_pay_auth_result_faceCode);
Log.d("xy_step5_371", "");
requestJson.put("channelType","wx");
Log.d("xy_step5_373", "");
requestJson.put("openId",open_face_pay_auth_result_openId);
//商品详情
JSONObject goodsDetail_item1 = new JSONObject();
goodsDetail_item1.put("goodsId","1");
goodsDetail_item1.put("goodsName","测试商品");
goodsDetail_item1.put("price","1");
goodsDetail_item1.put("goodsWeight","1.53");
JSONArray goodsDetail = new JSONArray();
goodsDetail.put(goodsDetail_item1);
//JSONObject .parseObject(str);
requestJson.put("goodsDetail",goodsDetail.toString());
//订单来源
/*
*
* 消费收银:
* 0支付码牌
* 1收款插件
* 2APP
* 3接口
* 4银行卡
* 5小程序
*
*
* * 消费退款:
* 0商户后台
* 1收款插件
* 2APP
* 3接口
* 4商城
* 5小程序
*
*
* * 会员充值:
* 0线上充值
* 1预存开卡
* 2手动充值
* 3营销赠送
*
*
* * 付费会员:
* 0付费卡
* 1付费等级
* * 付费卡券:
* 0优惠券
* 1次卡
* 2礼品卡
*
*
* * 线上商城:
* 0小程序
* 1微商城
* 2拼团
* 3外卖
* 4堂食
* 5酒店
*
*
* */
Log.d("xy_step5_422", "");
requestJson.put("orderSource","2");
//requestJson.put("rawdata","6E7DE469-E8AB-D219-8D1A-E79E433C36B3");
//requestJson.put("rawdata",rawdata);
//requestJson.put("channelType","WX");
//requestJson.put("deviceId","aa");
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson.toString());
Request request = new Request.Builder()
.url("https://shengke.bhzlkj.com/api/payment/payment/open_face_pay.html?site_id="+site_id+"&Fuel_PayID="+outTradeNo)
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Log.d("xy_step5_439", "");
Response response = client.newCall(request).execute();
Log.d("xy_step5_442_1", "");
String responseData=response.body().string();
Log.d("xy_step5_442", "result:"+responseData);
//解析
OpenFacePayResponseBean OpenFacePayResponse=new Gson().fromJson(responseData, OpenFacePayResponseBean.class);
if(OpenFacePayResponse.getData().getCode().equals("FAILED")){
//Log.d("xy_step5_489", "error:"+OpenFacePayResponse.getData().getSubMsg());
//Toast.makeText(myActivity, OpenFacePayResponse.getData().getSubMsg(), Toast.LENGTH_LONG).show();
Message msg = new Message();
msg.obj = OpenFacePayResponse.getData().getSubMsg();
toast_hanler.sendMessage(msg);
}
其中 requestJson.put("goodsDetail",goodsDetail.toString());地方如果不toString,则后端(以php为例)接收的是一个数组了(以thinkphp框架好像是5还是6来着,用的好像是/a在request->post("字段名/a")类似这样)
关键字词:安卓,json,okhttp,嵌套,解析,数组,字符串