okhttp获取失败响应 [英] okhttp get failure response

查看:807
本文介绍了okhttp获取失败响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的android客户端中实现了okhttp进行网络通话.

I've implemented okhttp in my android client for network calls.

当我收到失败响应时,我会收到失败代码和与该代码相关的文本作为消息,但我没有收到服务器发送给我的自定义失败响应. 在执行代码的失败响应中,收到的消息只是错误请求".

When i get a failure response i get the failure code and the text related to the code as a message but i don't get the custom failure response that the server sends me. In my failure response in the implemented code the message i get is just "Bad Request".

来自浏览器的相同响应如下.

Whereas the same response from the browser is as follows.

如何获取服务器退还给我的错误消息?

How do i get the error message the server is giving me back?

我的代码

private void executeCall(Request request, final ResponseListener listener) {
        mOKHttpClient.newCall(request)
                     .enqueue(new Callback() {
                         @Override
                         public void onFailure(Call call, IOException e) {                              
                             postFailure(listener, (String) call.request()
                                                                .tag(),e.toString());
                         }

                         @Override
                         public void onResponse(Call call, final Response response) throws IOException {
                             if(response.isSuccessful()) {
                                 String responseString = response.body().string();                                   
                                 postSuccess(listener, (String)call.request().tag(), responseString);
                             }
                             else {                                 
                                 postFailure(listener, (String)call.request().tag(),response.code()+","+response.message());
                             }
                         }
                     });
    }

这是我失败时的回应.

Here's my response in case of failure.

推荐答案

由于response.message()返回HTTP状态消息,您将不得不捕获body()的错误响应.

You will have to catch error response by body() because response.message() returns HTTP status message.

在您提供的屏幕截图中:

In the screen shot provided by you:

状态OkHttp中细分,例如response.code()表示HTTP状态代码为400,而response.message()表示HTTP状态消息为Bad Request

Status is broken down in OkHttp like response.code() for HTTP status code which is 400 in your case and response.message() for HTTP status message which is Bad Request in your case.

响应的主体(成功或失败)为response.body().如果要以String的形式获取它,请致电response.body().string().

The body of the response (be it success or failure) is response.body(). And if you want to get it as a String, then call response.body().string().

@Override
public void onResponse(Call call, final Response response) throws IOException {
     if(response.isSuccessful()) {
         String responseString = response.body().string();                                   
         postSuccess(listener, (String)call.request().tag(), responseString);
     }
     else {               
        String errorBodyString = response.body().string();                  
        postFailure(listener, (String)call.request().tag(),response.code()+","+errorBodyString);
     }
}

根据评论:

由于您想从响应中读取Message对象,因此请尝试执行以下操作:

Since you want to read Message object from the response, try to do like this:

JSONObject = new JSONObject (response.body().string());
String messageString =object.getString("Message");

这篇关于okhttp获取失败响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆