Cancel Queue Request In OKHTTP3

You can cancel current or queue request in OkHTTP3 buy adding a tag to your request and after that, you need to call bellow function to cancel a request for new request most useful if you need to request API on word change, for example, you can create new request when your search word change.

Add a tag to request which we find later for the cancel request

RequestBody body = RequestBody.create(MEDIA_TYPE, postData.toString());Request request = new Request.Builder()
.url(Urls.search)
.post(body)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.tag("preRequest")
.build();

To cancel Request we can find this tag in our OKHTTP client

for (Call call : client.dispatcher().runningCalls()) {
if (call.request().tag().equals("preRequest")) {
Log.e("cancle", "requestCancel");
call.cancel();
}
}

That’s it. You can read post here:- https://trinitytuts.com/tips/cancel-current-or-queue-request-in-okhttp3/

Thanks

|Published on September 4, 2019
Writer

Aneh

Trinity Tuts

Trinity Tuts