Trinitytuts Tech
5 Followers

Cancel current or queue request in OKHTTP3

people

Aneh Thakur

. 0 min read

This tip is small but very useful if you are working with OKHTTP3 library. With the help of this code, you can cancel current or queue request of OkHTTP 3.

OkHttpClient client = new OkHttpClient();
        MediaType MEDIA_TYPE = MediaType.parse("application/json");
        JSONObject postData = new JSONObject();
        try {
            postData.put("q", "My Query");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        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();

       // Cancel queue request 
        for (Call call : client.dispatcher().queuedCalls()) {
            if (call.request().tag().equals("requestKey"))
                call.cancel();
        }

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

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);
                call.cancel();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String mMessage = response.body().string();

                Search.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            /** Your response **/
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
  

Hope this small piece of code help you.


More Stories from

Aneh Thakur
Aneh Thakur.2 min read

Monetize react native app with Google Admob

Implement Google Mobile Ads in your React Native application to allows you to monetize your app with AdMob.

.
Monetize react native app with Google Admob
Aneh Thakur
Aneh Thakur.5 min read

Deploying React & Node.js App using GitHub Actions CI/CD

Learn how to deploy React and Node.Js application using CI/CD pipeline with Github Actions.

Deploying React & Node.js App using GitHub Actions CI/CD
Aneh Thakur
Aneh Thakur.1 min read

What is DevOps?

DevOps is a set of practices, tools, and a cultural philosophy that automate and integrate the processes between software development and IT teams. It emphasizes team empowerment, cross-team communication and collaboration, and technology automation.

.
What is DevOps?
Aneh Thakur
Aneh Thakur.2 min read

Different between find(), filter() and map() in javascript

Learn different between find(), filter() and map() in javascript with example and other array methods in javascript.

.
Aneh Thakur
Aneh Thakur.3 min read

All about MongoDB

MongoDB is an open-source document-oriented database that is designed to store a large scale of data and also allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval o

.
All about MongoDB