The right way to manage your network using retrofit and RX-java

I want to check if my mobile phone is connected to the Internet before calling my rx retooling service. If not connected, I want to return a fake response containing an error.

I finished the solution below using defer (), but I think it could 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; } }); } 
+8
android retrofit rx-java
source share
1 answer

You can implement a modified ErrorHandler, as described here: https://stackoverflow.com/a/416829/

And then handle the throw exception by the doOnError method or your onrrror method by your subscribers.

+2
source share

All Articles