I want to check if the mobile is connected to internet before invoking my rx retrofit service. If not connected I want to return a fake Response that contains an error.
I ended with the solution below, using defer(), but I think it can be better, any hints ?
private Observable<Response> checkNetwork(Observable<Response> retrofitService) {
return Observable.defer(new Func0<Observable<Response>>() {
@Override
public Observable<Response> call() {
if (!isOnline()) {
return Observable.just(Response.error(R.string.error_no_network_label)));
}
return retrofitService;
}
});
}
RxAndroid is an extension of RxJava and it contains the Android threads to be used in the Android Environment. To use RxJava in retrofit environment we need to do just two major changes: Add the RxJava in Retrofit Builder. Use Observable type in the interface instead of Call.
Rx gives you a very granular control over which threads will be used to perform work in various points within a stream. To point the contrast here already, basic call approach used in Retrofit is only scheduling work on its worker threads and forwarding the result back into the calling thread.
Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.
You can implement retrofit ErrorHandler like described here: https://stackoverflow.com/a/21055979/2927901
And then handle thrown exception in doOnError method or your subscribers's onError method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With