private void setupNotificationListener(){
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
flag=1;
//System.out.println(flag);
Map<String,Object> mapPost, mapContent;
Map<String,Object> mapTitle;
gson = new Gson();
list = (List) gson.fromJson(s, List.class);
postTitle = new String();
postContent=new String();
System.out.println("shiv class:"+list.size());
mapPost = (Map<String, Object>) list.get(0);
mapTitle = (Map<String, Object>) mapPost.get("title");
postTitle = (String) mapTitle.get("rendered");
mapContent = (Map<String, Object>) mapPost.get("content");
postContent = (String) mapContent.get("rendered");
if(!alreadyNotified(postTitle)){
Log.d("not notified","");
createNotif(postContent,postTitle);
saveNotificationKey(postTitle);
}else{
System.out.print("already notified");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("Error","Error");
}
});
rQueue = Volley.newRequestQueue(NotifierService.this);
rQueue.add(request);
NotifierService.getInstance().getRequestQueue().getCache().clear();
}
这是我的整个功能,我在其中发送截击请求并从URL解析json 现在我想在不增加请求队列的情况下每分钟获取一次json 有没有办法清空请求队列?
答案 0 :(得分:1)
如果您想在发生错误后立即取消所有请求:
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//....
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//....
mRequestQueue.cancelAll(new RequestQueue.RequestFilter() {
@Override
public boolean apply(Request<?> request) {
return true;
}
});
}
});
如果您想在离开活动后立即取消所有请求,请将取消代码放入onStop方法。 你应该这样做,否则你将在一个可能已经完成的活动中运行,并且在作业执行期间它可能会造成内存泄漏