okhttp post用json傳遞參數


//請求顯示數據
    private void getdata() {
        //開啟線程來發起網絡請求
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
添加一個json格式數據
                    MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                    JSONObject json = new JSONObject();
                    try {
                        json.put("serialNumber", serialNumber);
                        json.put("pageNum", pageNum);
                        json.put("pageSize", pageSize);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //1 . 拿到OkHttpClient對象
                    OkHttpClient client = new OkHttpClient();
                    //創建一個RequestBody(參數1:數據類型 參數2傳遞的json串)
                    RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                    //3 . 構建Request,將FormBody作為Post方法的參數傳入
                    Request request = new Request.Builder()
                            .url("http://172.28.60.97:8200/ZYGameServer_v2/app/v2/getChatInfoByPage")
                            .post(requestBody)
                            .build();

                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    getfeedback(responseData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //一個JSON對象——JSONObject{}
            //一個JSON數組——JSONArray[]
            private void getfeedback(String responseData) {
                try {
                    JSONObject jsonObject1 = new JSONObject(responseData);
                    JSONArray jsonArray = jsonObject1.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        //消息內容
                        String message = jsonObject.getString("message");
                        //消息類型(0:文本;1:圖片;;2:系統)
                        String type = jsonObject.getString("type");
                        //0:未讀;1:已讀
                        String read = jsonObject.getString("read");
                        //消息來源(0:用戶;1:平台)
                        String source = jsonObject.getString("source");
                        // 創建時間
                        long createTime = jsonObject.getLong("createTime");
                        myFeedbackDetailsModel.add(new MyFeedbackDetailsModel(message, type, read, source, createTime,null,null,null));
                    }
                    send_message = Message.obtain();
                    send_message.what = 100;
                    handler.sendMessage(send_message);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM