Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Cancel Retrofit request

I scroll through the official documentation of 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:

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 isCanceled in 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.

like image 539
Morozov Avatar asked Sep 20 '16 10:09

Morozov


2 Answers

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 Callback class to create your own class CancellableCallback 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

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();
}
like image 106
Amit Tiwari Avatar answered Sep 18 '22 17:09

Amit Tiwari


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 here for more.

like image 28
Pratik Butani Avatar answered Sep 18 '22 17:09

Pratik Butani