[英]How to cancel request with retofit2 and RxAndroid
I am using Retrofit 2.0 and Rx-android to load my APIs. 我正在使用Retrofit 2.0和Rx-android来加载我的API。 I follow the section
RxJava Integration with CallAdapter
at this site and it work fine. 我在这个网站上遵循
RxJava Integration with CallAdapter
它运行正常。 But, I don't know how to cancel a loading request with the observable object. 但是,我不知道如何使用可观察对象取消加载请求。 Please help to give me an idea.
请帮忙给我一个主意。
The only way to cancel RxJava Observable execution - unsubscribe from it. 取消RxJava Observable执行的唯一方法 - 取消订阅它。 RxJavaCallAdapter will delegate cancel to okhttp client.
RxJavaCallAdapter将取消委托给okhttp客户端。
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();
}
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.