Java – how to cancel a modification request

I scrolled through the official documentation retrofit and decided to implement something similar in my project, so that users can always choose to cancel downloading files and everything works normally Implement appropriate methods if the following are not specified:

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 the red underline is a failed method I don't know what the problem is, because we originally proposed the class call in this method (perhaps because we swear that I, not the class call, is retrofiterror?) Please tell me how to fix it I use modification 1.9, I don't need to continue to modify 2

Solution

In terms of memory, error Iscancelled() does not appear in retrofit If you want to be able to cancel a request, you can switch to them with call Retrofit 2 of the cancel () method, or in the current version of retrofit, you can extend the callback class to create your own class cancelablecallback, like this:

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

Then you can 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,callback);

Then cancel it like this:

if (some condition && callback != null) {
    callback.cancel();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>