OkHttp Post提交json数据

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/a136447572/article/details/86636612
new Thread() {
            @Override
            public void run() {
                // @Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头
                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                JSONObject json = new JSONObject();
                try {
                    json.put("EngineerId", getString(ShareFile.UID, ""));
                    json.put("EngorderNumber", orderNumber);
                    json.put("PID", pid);
                    json.put("RefundType", RefundType);
                    json.put("RefundReason", reason_text.getText().toString());
                    json.put("RefundImg", str);
                    json.put("RefundVideo", "");
                    json.put("discount", discount);
                    json.put("RefundState", "0");
                    json.put("shopkeeperId", shopkeeperId);
                    json.put("payMoney", ActualPayment);//tuikuanPriceString
                    json.put("refundNum", number);
                    json.put("RefundCategory", RefundCategory);
                    json.put("sidd", oldsidd + "");
                    if (RefundCategory.equals("2")) {
                        json.put("ExchangePID", pid);
                        json.put("ExchangeSidd", sidd + "");
                    } else {
                        json.put("ExchangePID", "");
                        json.put("ExchangeSidd", "");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //申明给服务端传递一个json串
                //创建一个OkHttpClient对象
                OkHttpClient okHttpClient = new OkHttpClient();
                //创建一个RequestBody(参数1:数据类型 参数2传递的json串)
                //json为String类型的json数据
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                //创建一个请求对象
//                        String format = String.format(KeyPath.Path.head + KeyPath.Path.waybillinfosensor, username, key, current_timestamp);
                Request request = new Request.Builder()
                        .url(NetUtil.saveEngRefundInfo())
                        .post(requestBody)
                        .build();

                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //DialogUtils.showPopMsgInHandleThread(Release_Fragment.this.getContext(), mHandler, "数据获取失败,请重新尝试!");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String string = response.body().string();
                        Log.i("info",string+"");
                        try {
                            JSONObject json = new JSONObject(string);
                            strddd = json.optString("msg");
                            handler.sendEmptyMessage(0x11);
                            if (json.optString("flag").equals("00")) {
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }.start();

猜你喜欢

转载自blog.csdn.net/a136447572/article/details/86636612