如何取消或忽略已经开始的排球请求?

时间:2017-08-18 12:04:53

标签: android android-volley

我知道可以取消Volley挂号请求,那已经开始的那个怎么样?

1 个答案:

答案 0 :(得分:1)

试试这个:

 RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
 String url = "Your url";

 StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Toast.makeText(MainActivity.this, "Response is: "+ response.substring(0,5), Toast.LENGTH_SHORT).show();
                        }
                    },

                    new Response.ErrorListener() {
                       @Override
                       public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this, "That didn't work!", Toast.LENGTH_SHORT).show();
                       }
                    });
stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
queue.cancelAll(stringRequest);