• 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();
        }
    

      

  • 相关阅读:
    JAVA——汉诺塔
    JAVA与MySQL连接并显示、管理表格实例
    2019沈阳网选——模拟
    CodeforcesRound#553(Div. 2)(A-D题解)
    CodeforcesRound#551(Div. 2)(A-C题解)
    CodeforcesGlobalRound2(Div.2)ABCE题解
    EducationalCodeforcesRound62(Div. 2)(A-D题解)
    博客搬家
    文本分类基本流程
    卡方检验应用-特征选择
  • 原文地址:https://www.cnblogs.com/wang-jingyuan/p/12174055.html
Copyright © 2020-2023  润新知