UPDATE: In OkHttp 3, it is much simpler using Dispatcher , which has a cancelAll() method. The dispatcher returns from OkHttpClient.dispatcher() .
Old solution: The only way to do this (I could find) is to subclass OkHttpClient and use it with Retrofit.
class OkHttpClientExt extends OkHttpClient { static final Object TAG_CALL = new Object(); @Override public Call newCall(Request request) { Request.Builder requestBuilder = request.newBuilder(); requestBuilder.tag(TAG_CALL); return super.newCall(requestBuilder.build()); } }
The next line cancels all requests with the tag TAG_CALL . Since the class above sets TAG_CALL for all requests, therefore all requests are canceled.
retrofit.client().cancel(OkHttpClientExt.TAG_CALL);
Shubhadeep chaudhuri
source share