如何使用 retofit2 和 RxAndroid 取消请求

How to cancel request with retofit2 and RxAndroid

我正在使用 Retrofit 2.0 和 Rx-android 加载我的 API。我按照 this siteRxJava Integration with CallAdapter 部分进行操作,它工作正常。但是,我不知道如何使用可观察对象取消加载请求。请大家帮忙出出主意。

取消 RxJava Observable 执行的唯一方法 - 取消订阅。 RxJavaCallAdapter 会将取消委托给 okhttp 客户端。

那么,你就这么简单吧:

Subscription subscription = getObservable().subscribe();

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

您可以查看代码here。如果代码

具体是这些行
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();
  }
}));