Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - cancel Volley request

I am using Android Volley lib in my project to execute network requests, all works very well but I have some troubles with the "cancel" feature of this lib. I explain my issue..

I've an activity, where I'm executing the request at OnCreate method, the request is called, no problem. But to be sure that the cancel method works, I wanted to test and try 2 things :

  1. I fire my request and just after cancel it like this :

    MySingleton.getMyData("urltocall", getDataListener, requestTag); MySingleton.getRequestQueue().cancelAll(requestTag);

This one works! The cancel is called (I can see it too in the Request class of Volley) :

public void cancel() {
        mCanceled = true; // my breakpoint is called here
    }
  1. I fire my request and just after call finish() method of my activity and in onDestroy and/or onStop method of the activity, I'm calling the same code :

    MySingleton.getMyData("urltocall", getDataListener, requestTag); MySingleton.getRequestQueue().cancelAll(requestTag);

But this doesn't work!

The requestTag is not null and well passed to Volley, so I can't understand why the first method works but not the other one... Knowing that my purpose is to cancel request when onDestroy is called..

Thanks for your help

like image 355
ejay Avatar asked Mar 14 '15 16:03

ejay


People also ask

How to use simple volley request in Android?

How to use simple volley request in android? How to use simple volley request in android? This example demonstrate about How to use simple volley request in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

What is the use of volley in Salesforce?

Another useful tool in Volley is the ability to cancel the requests. Canceling requests is useful when the user closes the app or performs an action that results in not using the responses of the Volley requests. There is no sense executing the remaining requests in the queue, so we cancel them in the onStop () method.

Where can I find volley library in Android?

It is available through AOSP(Android Open Source Project) repository. The volleylibrary has the features like automatic scheduling of network request, multiple concurrent connections, request prioritization, cancel/block a request, easier management of UI with data fetched asynchronously from the network and also offers easier customization.

What are the key classes of Android volley?

Key Classes of Android Volley The following are the Key classes of Volley: – RequestQueue: A Queue containing the Network/HTTP Requests that needs to be made. – Request: A Base Class which contains Network related information like HTTP Methods. – StringRequest: HTTP Request where the response is parsed a String. View Source


2 Answers

My guess would be that in the first case, the request has only been added to the RequestQueue, which is why calling cancelAll() works. In the second case, there is a slight delay between starting the request and pausing/destroying the Activity: in that delay, the HTTP request has begun. You may not be aware that calling cancelAll() works only for those requests that are still in the RequestQueue. It doesn't work for an HTTP request that has already begun, there is no way to stop that.

Having said that, the documentation here implies that once a request is cancelled (i.e. by calling cancel()), it can effectively be treated as though the web service had never been called in the first place. The callbacks associated with the particular request would not be invoked (although the response received would likely be held in the local cache).

like image 140
Y.S Avatar answered Oct 16 '22 06:10

Y.S


You can cancel a Request attached to a TAG. So basically you have to add a tag with every Request.

yourRequestObject.setTag(TAG);

RequestQueue mRequestQueue;

mRequestQueue.cancelAll(TAG);

This way you can cancel all the Request with a particular TAG.

like image 22
peeyush pathak Avatar answered Oct 16 '22 06:10

peeyush pathak