Skip to content Skip to sidebar Skip to footer

Delete Requests Remaining In The Queue After Cancelall

In the code below after the CancelAll and Stop, the request that added afterwards in the queue, will be immediately execute after the start command. How can i remove the last reque

Solution 1:

As you queue reference is a local variable so you need to move it outside and since you are using it in activity so declare it like

private RequestQueue queue;

..oncreate(..){
    //... code
    queue = Volley.newRequestQueue(this);
}

and create a separate method to cancel all requests as

voidcancelAllQueuedRequests(){
    queue.cancelAll("a");
    queue.stop();
    queue.start();
}

call cancelAllQueuedRequests wherever you want and add requests like this

String url ="some url";

// Request a string response from the provided URL.StringRequest stringRequest = newStringRequest(Request.Method.GET, url,
        newResponse.Listener<String>() {
            @OverridepublicvoidonResponse(String response) {

                Log.d("response", response);
            }
        }, newResponse.ErrorListener() {
    @OverridepublicvoidonErrorResponse(VolleyError error) {
        Log.d("VolleyError", error.toString());
        cancelAllQueuedRequests();
    }
});

// Add the request to the RequestQueue.

stringRequest.setTag("a");
queue.add(stringRequest);
//queue.start(); no need

Post a Comment for "Delete Requests Remaining In The Queue After Cancelall"