【问题标题】:How to Cancel Retrofit request如何取消改造请求
【发布时间】:2016-09-20 20:03:13
【问题描述】:

我浏览了 Retrofit 的官方 documentation 并决定在我的项目中实现类似的功能,以便用户始终可以选择取消下载文件,并且一切都会正常工作。实施适当的方法,其中未能规定以下:

service.upload1(file1, str, stringMap, new Callback<ImageUpload>() {
            @Override
            public void success(final ImageUpload imageUpload, Response response) {
                mRecyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        ...
                });
            }

            @Override
            public void failure(RetrofitError error) {
                if (error.isCanceled()) {
                    Log.e(TAG, "request is cancelled");
                } else {
                    Log.e(TAG, "other larger issue, i.e. no network connection?");
                }
            }

但在失败方法中用红色下划线isCanceled。我不明白是什么问题,因为我们最初提出的这个方法是 class Call(可能是因为骂我而不是 class Call 是 RetrofitError?)请告诉我如何解决。 我用的是改造 1.9,我不需要继续改造 2。

【问题讨论】:

  • isCanceled() 是该类型的现有方法,拼写是否正确?
  • 您使用的是改造 1 还是改造 2?我认为 isCanceled 是在 Retrofit2 中引入的
  • 仔细阅读文档............isCancelled() 方法属于 retrofit2.Call 类而不是 RetorfitError 类

标签: java android retrofit


【解决方案1】:

据我记忆,error.isCanceled() 似乎没有出现在 Retrofit 中。如果您希望能够取消请求,您可以切换到具有 call.cancel() 方法的 Retrofit 2,或者在当前版本的 Retrofit 中,您可以扩展 Callback 类以创建您自己的类 CancellableCallback像这样:

public class CancellableCallback<T> implements Callback<T> {

    private Callback<T> callback;

    private boolean canceled;

    private CancellableCallback() {}

    public CancellableCallback(Callback<T> callback) {
        this.callback = callback;
        canceled = false;
    }

    public void cancel() {
        canceled = true;
        callback = null;
    }

    @Override
    public void success(T o, Response response) {
        if (!canceled) {
            callback.success(o, response);
        }
    }

    @Override
    public void failure(RetrofitError error) {
        if (!canceled) {
            callback.failure(error);
        }
    }
}

编辑

然后你可以像这样使用它:

CancellableCallback callback = new Callback<ImageUpload>() {
            @Override
            public void success(final ImageUpload imageUpload, Response response) {
                mRecyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        ...
                });
            }

            @Override
            public void failure(RetrofitError error) {

            }
    };

service.upload1(file1, str, stringMap, callback);

然后像这样取消它:

if (some condition && callback != null) {
    callback.cancel();
}

【讨论】:

  • 我不太明白你在说什么,我会在我的答案中添加更多解释。
  • 如果请求上传大文件会发生什么? :/ 文件是否应该在用户不知情的情况下继续上传?
【解决方案2】:

您可以像这样取消请求:

// something happened, for example: user clicked cancel button
service.cancel();  

并获得取消的回调,如:

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
       if (call.isCanceled()) {
           Log.e(TAG, "request was cancelled");
       }
       else {
           Log.e(TAG, "other larger issue, i.e. no network connection?");
       }
}

您可以查看here了解更多信息。

【讨论】:

  • 谢谢,但这个例子适合改造 2。在我的应用程序中,我使用了 1.9。
  • 什么是服务?那是什么课程类型?
  • @AmirZiarati service 是一个改造2 Call,我们通常像service.enqueue(new Callback&lt;ResponseBody&gt;() { ... } 一样使用它
【解决方案3】:

您应该为此使用 Retrofit 2 并更改接口方法签名以返回 Call 对象。要发出请求,请调用 enqueue 方法。之后可以调用cancel方法取消请求。

【讨论】:

    【解决方案4】:

    你应该切换到 retrofit2 ,然后写这样的东西 -

    service.upload1(file1, str, stringMap, new Callback<ImageUpload>() {
            @Override
            public void onResponse(Call<ImageUpload> call, Response<Topics> response) {
    
                //retrieve your resbonse body here like this -
                //   ImageUpload imageUpload = response.body();
                mRecyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        ...
                });
            }
    
            @Override
            public void onFailure(Call<ImageUpload> call, Throwable t) {
                if (call.isCanceled()) {
                    Log.e(TAG, "request is cancelled");
                } else {
                    Log.e(TAG, "other larger issue, i.e. no network connection?");
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2016-12-08
      • 2019-05-15
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多