I recently upgraded to OkHttp3, and noticed that you could no longer cancel a Call by tag directly from the Client. This has to be handled by the application now.
St
Use the following utility class to cancel a running or queued Call
by 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 case here: https://gist.github.com/RyanRamchandar/64c5863838940ec67f03