简体   繁体   English

如何让反应机构改造例外?

[英]How to get response body to retrofit exception?

I am trying to connect to rest service via retrofit in android application. 我试图通过Android应用程序中的改造连接到休息服务。 I am getting responses. 我得到了回复。 But when there is some error response from the service, conversion exception occurs and now I want to do some actions based on the response body. 但是当服务有一些错误响应时,会发生转换异常,现在我想根据响应主体做一些操作。 But I am getting response body as NULL. 但我得到的响应正文为NULL。 But retrofit log has a error message. 但改装日志有一条错误消息。 Why is this happening. 为什么会这样呢?

D/Reftofit log(24856): OkHttp-Received-Millis: 1397527055676
D/Reftofit log(24856): OkHttp-Response-Source: NETWORK 200
D/Reftofit log(24856): OkHttp-Sent-Millis: 1397527055492
D/Reftofit log(24856): Server: Apache/2.2.22 (Ubuntu)
D/Reftofit log(24856): X-Powered-By: PHP/5.3.10-1ubuntu3.10
D/Reftofit log(24856): {"result":"Invalid Token ID"}

Code: 码:

public void failure(RetrofitError retrofitError) {
    String response = null;
    TokenError tokenError = (TokenError) retrofitError.getBodyAs(TokenError.class);
    response = tokenError.getErrorDetails();
    Log.e(TAG, response);
    if (response != null && response.contains("Invalid Token ID")) {
        GroupDataProvider.getInstance().onFailure();
    }

}

Here I am getting tokenError as null . 这里我将tokenError作为null I don't know why? 我不知道为什么? Do I need to set something with rest adapter so that the response will be passed to retrofit error object. 我是否需要使用rest适配器设置某些内容,以便将响应传递给改进的错误对象。

Try this code: 试试这段代码:

@Override
public void failure(RetrofitError error) {
    String json =  new String(((TypedByteArray)error.getResponse().getBody()).getBytes());
    Log.v("failure", json.toString());
}

with Retrofit 2.0 使用Retrofit 2.0

@Override
public void onFailure(Call<Example> call, Throwable t) {
    String message = t.getMessage();
    Log.d("failure", message);
}

if error in String format: 如果字符串格式错误:

public Sring getErrorBodyAsString(RetrofitError error) {    
      return (String) error.getBodyAs(String.class)
}

if you need custom object: 如果你需要自定义对象:

class ErrorResponse {
   @SerializedName("error")
   int errorCode;

   @SerializedName("msg")
   String msg;
}

public T getErrorBody(RetrofitError error, Class<T> clazz) {    
      return (T) error.getBodyAs(clazz);
}

Your server should return 4xx HTTP error code to get this working. 您的服务器应返回4xx HTTP错误代码以使其正常工作。

When your server returns HTTP 200 this mean successful response and it will be processed with onSuccess branch. 当您的服务器返回HTTP 200时,这意味着成功的响应,它将使用onSuccess分支进行处理。 Likely your object which you pass to Callback<Object> should be ready to handle both success and error result. 您传递给Callback<Object>可能已准备好处理成功和错误结果。

To make sure this is the case you can check if RetrofitError is actually a JsonParseException by adding next snippet: 要确保这种情况,您可以通过添加下一个片段来检查RetrofitError是否实际上是JsonParseException

public void failure(RetrofitError error) {
    Log.v(TAG, error.getMessage());
};

you need use getErrorStream() for this. 你需要使用getErrorStream()。

If the HTTP response indicates that an error occurred, getInputStream() will throw an IOException. 如果HTTP响应指示发生错误,则getInputStream()将抛出IOException。 Use getErrorStream() to read the error response. 使用getErrorStream()来读取错误响应。 The headers can be read in the normal way using getHeaderFields(). 可以使用getHeaderFields()以正常方式读取标头。

Ref: github issue 参考: github问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM