Retrofit 2.0 & RxJava - Custom Error Handler

▼魔方 西西 提交于 2019-12-13 07:11:43

问题


I'm using Retrofit 2.0 along with RxJava and I need to make a custom error handler so that I can get the HTTP error code. This is the example that I get, but it won't work on Retrofit 2.0, since RetrofitError is removed.

@Override
public void onError(Throwable e) {
 if (e instanceof RetrofitError) {
    if (((RetrofitError) e).isNetworkError()) {
        //handle network error
    } else {
        //handle error message from server
    }
 }
}

Is there any way that I can do this? Thanks.


回答1:


I'm sorry that I didn't read the documentation carefully, so I just find a simple answer that works

@Override
 public void onError(Throwable e) {
    HttpException exception = (HttpException) e;
    Toast.makeText(getApplicationContext(), exception.code() + "", Toast.LENGTH_SHORT).show();
 }


来源:https://stackoverflow.com/questions/33074927/retrofit-2-0-rxjava-custom-error-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!