How to cancel a request using retofit2 and RxAndroid

I use Retrofit 2.0 and Rx-android to load my APIs. I follow the RxJava Integration with CallAdapter on this site and it works great. But I do not know how to cancel a download request with an observable object. Please help me give me an idea.

+6
source share
1 answer

The only way to cancel RxJava Observable is to unsubscribe. RxJavaCallAdapter delegates cancellation to okhttp client.

So you just do something like this:

 Subscription subscription = getObservable().subscribe(); //When you want to stop execution subscription.unsubsribe(); 

Here you can check the code here . Specifically, these lines, if the code

 final Call<T> call = originalCall.clone(); // Attempt to cancel the call if it is still in-flight on unsubscription. subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { call.cancel(); } })); 
+17
source

All Articles