Handle Network error with Retrofit observable

前端 未结 4 2125
失恋的感觉
失恋的感觉 2021-02-18 13:13

When using Observables with Retrofit how do you handle Network failure?

Given this code:

Observable observable = api.getApiService(         


        
4条回答
  •  一生所求
    2021-02-18 13:49

    My problem was actually elsewhere in my code. Handling network errors with rxJava + Retrofit is very easy as it just throws a RetrofitError in the onError method:

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

提交回复
热议问题