解决Okhttp3使用post请求后的response.body()为空

Posted hequnwang10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决Okhttp3使用post请求后的response.body()为空相关的知识,希望对你有一定的参考价值。

一、问题

代码如下:

public void login(View view) {
        loginUser = new LoginUser();
        loginUser.setUserName(username.getText().toString());
        loginUser.setUserPassword(MD5.encrypt(password.getText().toString()));
        new Thread(new Runnable() {
            @Override
            public void run() {
                MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                JSONObject jsonObject = new JSONObject();
                OkHttpClient httpClient = new OkHttpClient();
                try {
                    jsonObject.put("userName",loginUser.getUserName());
                    jsonObject.put("userPassword",loginUser.getUserPassword());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(jsonObject));
                String url = "http://114.213.208.85:8001/server/user/login";
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();

                Call call = httpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("whq登录","失败了");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.d("whq登录",response+"---------response---------");
                        Log.d("whq登录",response.message()+"---------message---------");
                        Log.d("whq登录",response.body().toString()+"------------------");
                    }
                });
            }
        }).start();
    }

输出如下:

2021-07-28 21:08:51.170 19044-19160/com.example.login D/whq登录: Response{protocol=http/1.1, code=200, message=, url=http://ip/server/user/login}---------response---------
2021-07-28 21:08:51.170 19044-19160/com.example.login D/whq登录: ---------message---------
2021-07-28 21:08:51.170 19044-19160/com.example.login D/whq登录: okhttp3.internal.http.RealResponseBody@b761df1------------------


这里的message为空;

二、解决

将response.body().toString()赋给一个变量就可以了,将这个变量打印出来

public void login(View view) {
        loginUser = new LoginUser();
        loginUser.setUserName(username.getText().toString());
        loginUser.setUserPassword(MD5.encrypt(password.getText().toString()));
        new Thread(new Runnable() {
            @Override
            public void run() {
                MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                JSONObject jsonObject = new JSONObject();
                OkHttpClient httpClient = new OkHttpClient();
                try {
                    jsonObject.put("userName",loginUser.getUserName());
                    jsonObject.put("userPassword",loginUser.getUserPassword());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(jsonObject));
                String url = "http://114.213.208.85:8001/server/user/login";
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();

                Call call = httpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("whq登录","失败了");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String MyResult = response.body().string();
                        Log.d("whq登录",response+"---------response---------");
                        Log.d("whq登录",response.message()+"---------message---------");
                        Log.d("whq登录",response.body().toString()+"------------------");
                        Log.d("whq登录",MyResult+"-----------MyResult-------");
                    }
                });
            }
        }).start();
    }

输出:

以上是关于解决Okhttp3使用post请求后的response.body()为空的主要内容,如果未能解决你的问题,请参考以下文章

[技术博客]OKhttp3使用get,post,delete,patch四种请求

使用 MockWebServer 模拟嵌套的改造 api 调用

使用 MockWebServer 模拟嵌套的改造 api 调用

okhttp3 post请求基本使用以及超时重连

Okhttp3、http2多路复用POST请求高峰负载时响应时间长

field load respone data