当前位置:首页 > Web开发 > 正文

//请求显示数据private void getdata() {//开启线程来发起网络请求new Thread(new

2024-03-31 Web开发

//请求显示数据 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(); }

  

okhttp post用json通报参数

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31933.html