在CancelAll之后删除队列中剩余的请求 [英] Delete requests remaining in the Queue after CancelAll
本文介绍了在CancelAll之后删除队列中剩余的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的CancelAll和Stop之后的代码中,之后添加到队列中的请求将在start命令之后立即执行.
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 request/requests that was inserted in queue?
final RequestQueue queue = Volley.newRequestQueue(this);
String url ="";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("VolleyError", error.toString());
}
});
// Add the request to the RequestQueue.
stringRequest.setTag("a");
queue.cancelAll("a");
queue.stop();
queue.add(stringRequest);
queue.start();
推荐答案
由于您queue
引用是局部变量,因此您需要将其移到外部,并且由于您正在活动中使用它,因此请像
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
void cancelAllQueuedRequests(){
queue.cancelAll("a");
queue.stop();
queue.start();
}
随时随地致电cancelAllQueuedRequests
并添加这样的请求
call cancelAllQueuedRequests
wherever you want and add requests like this
String url ="some url";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("VolleyError", error.toString());
cancelAllQueuedRequests();
}
});
// Add the request to the RequestQueue.
stringRequest.setTag("a");
queue.add(stringRequest);
//queue.start(); no need
这篇关于在CancelAll之后删除队列中剩余的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文