Handle Network error with Retrofit observable

前端 未结 4 2128
失恋的感觉
失恋的感觉 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:58

    As of Retrofit2 and RxJava2 there is no more RetrofitError exception. And its successor HttpException only represents HTTP error response codes. Network errors should be handled through IOException.

     @Override
     public void onError(Throwable e) {
         if (e instanceof IOException) {
                //handle network error
         } else if (e instanceof HttpException) {
                //handle HTTP error response code
         } else {
                //handle other exceptions
         } 
     }
    

提交回复
热议问题