okhttp post transfer parameters json

// the display data request 
    Private void getData () { 
        // Open thread to request the network initiates 
        new new the Thread (the Runnable new new () { 
            @Override 
            public void RUN () { 
                the try { 
add a data format json 
                    MediaType JSON = MediaType.parse ( " file application / JSON; charset = UTF-. 8 "); 
                    the JSONObject the JSONObject JSON new new = (); 
                    the try { 
                        json.put (" the serialNumber ", the serialNumber); 
                        json.put (" pageNum ", pageNum); 
                        json.put (" the pageSize ", the pageSize); 
                    } the catch (JSONException E) { 
                        e.printStackTrace (); 
                    }
                    // get a target OkHttpClient 
                    OkHttpClient = new new OkHttpClient Client (); 
                    // Create a requestBody (Parameter 1: 2 type parameter data passed json string) 
                    requestBody requestBody = RequestBody.create (the JSON, String.valueOf (json)) ; 
                    // 3 Construction Request, as a parameter to the Post method FormBody incoming. 
                    the Request = Request new new Request.Builder () 
                            .url ( "http://172.28.60.97:8200/ZYGameServer_v2/app/v2/getChatInfoByPage") 
                            . POST (requestBody) 
                            .build (); 

                    the Response Response = client.newCall (Request) .execute (); 
                    String response.body the responseData = () String ().;
                    getFeedback (the responseData); 
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                } 
            } 

            // {a} JSON object --JSONObject 
            // a JSON array --JSONArray [] 
            Private void getFeedback (String the responseData) { 
                the try { 
                    = new new jsonObject1 the jSONObject the jSONObject (the responseData); 
                    the JSONArray JSONArray = jsonObject1.getJSONArray ( "Data"); 
                    for (int I = 0; I <jsonArray.length (); I ++) {
                        JsonObject = jsonArray.getJSONObject the JSONObject (I); 
                        / /Message content
                        = JsonObject.getString Message String ( "Message"); 
                        // message type (0: Text; 1: 2 ;; Image: System) 
                        String = jsonObject.getString type ( "type"); 
                        // 0: unread; 1 : read 
                        String = jsonObject.getString the read ( "the read"); 
                        // sources (0: user; 1: platform) 
                        String source = jsonObject.getString ( "source"); 
                        // create a time 
                        long createTime = jsonObject.getLong ( "createTime"); 
                        myFeedbackDetailsModel.add (new 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();
    }

  

Guess you like

Origin www.cnblogs.com/wang-jingyuan/p/12174055.html