Skip to content

Instantly share code, notes, and snippets.

@paulocoutinhox
Created November 19, 2018 13:57
Show Gist options
  • Save paulocoutinhox/bc1c85151be5e681f8c9ab903f727ed9 to your computer and use it in GitHub Desktop.
Save paulocoutinhox/bc1c85151be5e681f8c9ab903f727ed9 to your computer and use it in GitHub Desktop.
OkHttp utility class to stop call by tag
package com.prsolucoes.android;
import android.text.TextUtils;
import okhttp3.Call;
import okhttp3.OkHttpClient;
public class OkHttpUtils {
public static void cancelCallWithTag(OkHttpClient client, String tag) {
if (client == null) {
return;
}
if (TextUtils.isEmpty(tag)) {
return;
}
for (Call call : client.dispatcher().queuedCalls()) {
if (call.request().tag() != null && call.request().tag().equals(tag))
call.cancel();
}
for (Call call : client.dispatcher().runningCalls()) {
if (call.request().tag() != null && call.request().tag().equals(tag))
call.cancel();
}
}
public static void cancelAll(OkHttpClient client) {
if (client == null) {
return;
}
for (Call call : client.dispatcher().queuedCalls()) {
call.cancel();
}
for (Call call : client.dispatcher().runningCalls()) {
call.cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment