How to cancel request with retofit2 and RxAndroid

自作多情 提交于 2019-12-01 01:58:56

问题


I am using Retrofit 2.0 and Rx-android to load my APIs. I follow the section RxJava Integration with CallAdapter at this site and it work fine. But, I don't know how to cancel a loading request with the observable object. Please help to give me an idea.


回答1:


The only way to cancel RxJava Observable execution - unsubscribe from it. RxJavaCallAdapter will delegate cancel to okhttp client.

So, you simple do smth like this:

Subscription subscription = getObservable().subscribe();

//When you want to stop execution
subscription.unsubsribe();

You can check out the code here. Concretely these lines if 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();
  }
}));


来源:https://stackoverflow.com/questions/34199769/how-to-cancel-request-with-retofit2-and-rxandroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!