java - 在 CancelAll 之后删除队列中剩余的请求
问题描述
下面的代码中,在CancelAll和Stop之后,后面添加到队列中的请求,会在start命令之后立即执行。
如何删除插入队列中的最后一个请求/请求?
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
引用的是一个局部变量,因此您需要将其移到外部,并且由于您在活动中使用它,因此将其声明为
private RequestQueue queue;
..oncreate(..){
//... code
queue = Volley.newRequestQueue(this);
}
并创建一个单独的方法来取消所有请求
void cancelAllQueuedRequests(){
queue.cancelAll("a");
queue.stop();
queue.start();
}
cancelAllQueuedRequests
随时随地拨打电话并添加这样的请求
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
推荐阅读
- angular6 - 如何在角度6的双引号内插入属性
- angularjs - 通过 Node JS 中的 URL 进行多个输入
- java - javax.mail 无法在 Marshmallow 或更高版本的系统上运行
- reactjs - 酶的浅层和镶嵌
- android - 相机照片未显示离子 3
- hyperledger-composer - 在 API 响应中找不到区块链数据
- php - How to parse this json getting in the response
- android - 如何在aysnctask中执行长循环?
- r - 使用 R ggplot2 分组条形图
- appium - 无法单击 Appium 中未显示的单元格中的元素 - IOS 使用 Java-Client