Java 如何取消改造请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39592084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:17:02  来源:igfitidea点击:

How to Cancel Retrofit request

javaandroidretrofit

提问by Morozov

I scroll through the official documentationof Retrofit and decided to implement something like this in my project, so that the user always has the option to cancel the download file and everything would work correctly. Implement appropriate methods, where the failure to prescribe the following:

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

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?");
                }
            }

But underlines in red isCanceledin failure method. I do not understand what's the problem, because this method we initially proposed class Call(perhaps because swears at me instead of class Call is RetrofitError?) Please tell me how to fix. I use retrofit 1.9, and i don't need go on retrofit 2.

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

采纳答案by Amit Tiwari

error.isCanceled()does not seem to be there in Retrofit as far as I remember. If you want to be able to cancel a request, you can either switch to Retrofit 2 where they have a call.cancel()method or in the current version of Retrofit, you could extent the Callbackclass to create your own class CancellableCallbacklike this:

error.isCanceled()据我所知,Retrofit 中似乎没有。如果你希望能够取消一个请求,你可以切换到 Retrofit 2,那里有一个call.cancel()方法,或者在当前版本的 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);
        }
    }
}

Edit

编辑

You can then use it like this:

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

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);

And then cancel it like this:

然后像这样取消它:

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

回答by Marius Kaunietis

You should use Retrofit 2 for that and change interface method signature to return a Callobject. To do a request, you call enqueuemethod. After that, you can call cancelmethod to cancel the request.

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

回答by Devansh Kumar

You should switch to retrofit2 , and then write something like this -

你应该切换到 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?");
            }
        }

回答by Pratik Butani

You can Cancel Request like:

您可以取消请求,例如:

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

and get callback of Cancel like:

并获得取消的回调,如:

@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?");
       }
}

You can check herefor more.

您可以在此处查看更多信息。