Handling Network Error in Rxjava 2 - Retrofit 2
See original GitHub issueHow 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
Issue Analytics
- State:
- Created 7 years ago
- Comments:23 (4 by maintainers)
Top Results From Across the Web
Retrofit 2— Best way for Handling Network Error - Medium
Retrofit 2 — Best way for Handling Network Error. Getting bored to check internet connectivity before each network call again and again ?...
Read more >Handling Network Error in Rxjava 2 - Retrofit 2 - Stack Overflow
How can we handle different network errors in Rxjava2 ? We used to check the instance of the throwable if it's of IOException...
Read more >Legacy Project Refactoring: Handling API Errors with Retrofit2 ...
Conclusion. Handling API errors with Retrofit2 and RxJava is a great solution for a few reasons. First, you get rid of excessive, repeating...
Read more >Centralized Network Error Handling Retrofit - AndroidWave
We'll learn centralized network error handling in Android. ... 2. Do Retrofit configuration with OkHttp. Go to the network folder in source ...
Read more >Custom Error Handling with RxJava & Retrofit 2 - Dan Stone
We've made our own subclass of Exception that holds the message and cause. This form of Exception is called when there's an internet...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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’