OkHttpClient cannot cancel call by tag

I recently upgraded to OkHttp3 and noticed that you can no longer cancel the Call on tag directly from the client. This application should now be processed.

It indicates CHANGELOG :

Cancellation of call packets is now the responsibility of the application. The API for canceling calls by tag has been removed and replaced with a more general mechanism. The dispatcher now provides all calls in flight using the runCalls () and queuedCalls () methods. You can write code that selects calls using a tag, host or any other and calls Call.cancel () on those that are no longer needed.

I myself reply to this post using my simple utility method to cancel the launch or Call by by turns.

+4
source share
1 answer

Use the following utility class to cancel an executable or queued tag Callby tag:

public class OkHttpUtils {
    public static void cancelCallWithTag(OkHttpClient client, String tag) {
        // A call may transition from queue -> running. Remove queued Calls first.
        for(Call call : client.dispatcher().queuedCalls()) {
            if(call.request().tag().equals(tag))
                call.cancel();
        }            
        for(Call call : client.dispatcher().runningCalls()) {
            if(call.request().tag().equals(tag))
                call.cancel();
        }
    }
}

I created an example with a test example: https://gist.github.com/RyanRamchandar/64c5863838940ec67f03

+7
source

All Articles