RxJava: Handling Network Error in Rxjava 2 - Retrofit 2
How can we handle different network errors in Rxjava2 ?
We used to check the instance of the throwable if it’s of IOException or HttpException back with Rxjava 1 ,however, in RxJava 2 the throwable error is of type GaiException.
code snippet
RestAPI restAPI = RetrofitHelper.createRetrofitWithGson().create(RestAPI.class);
Observable<BaseResponseTourPhoto> observable = restAPI.fetchData("Bearer " + getAccessToken(), "2", "" + page)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Disposable subscription = observable.subscribe(BaseResponse-> {
onLoadingFinish(isPageLoading, isRefreshing);
onLoadingSuccess(isPageLoading, BaseResponse);
writeToRealm(BaseResponse.getData());
}, error -> {
onLoadingFinish(isPageLoading, isRefreshing);
onLoadingFailed(error);
});
mCompositeDisposable = new CompositeDisposable();
mCompositeDisposable.add(subscription);
unsubscribeOnDestroy(mCompositeDisposable);
private void onLoadingFailed(Throwable error) {
try {
// We had non-200 http error
if (error instanceof HttpException) {
HttpException httpException = (HttpException) error;
Response response = httpException.response();
Log.i(TAG, error.getMessage() + " / " + error.getClass());
}
// A network error happened
if (error instanceof IOException) {
Log.i(TAG, error.getMessage() + " / " + error.getClass());
}
Log.i(TAG, error.getMessage() + " / " + error.getClass());
} catch (Exception e) {
Log.i(TAG, e.getMessage());
}
}```
reference: https://github.com/square/retrofit/issues/690
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 23 (4 by maintainers)
@JakeWharton the same issue There is my list of dependencies:
But my main issue is – when start internet request with no internet connection, I can’t handle this error and app crashes.
You can use ‘com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0’
Use 2.3.0
On Thu, Jun 8, 2017 at 11:53 AM eGorets notifications@github.com wrote:
Add this statement to Application class
@JakeWharton the same issue
no internet connection, repeated requests with exception
I can’t handle this error and app crashes.