Created
November 19, 2018 13:57
-
-
Save paulocoutinhox/bc1c85151be5e681f8c9ab903f727ed9 to your computer and use it in GitHub Desktop.
OkHttp utility class to stop call by tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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