0

i want to cancel volley request if there is no response in specific time

StringRequest stringRequest = new StringRequest(Request.Method.POST, test_check_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("resultarray");
                JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                Calendar calendar = Calendar.getInstance();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> map = new HashMap<>();
            map.put("test_id","");
            map.put("chap_id","");]
            map.put("type","check_test");
            return map;
        }
    };
    stringRequest.setTag(TAG);
    MySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);

i tried doing this:

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (MySingleton.getInstance(getContext()).getRequestQueue() != null) {
                MySingleton.getInstance(getContext()).getRequestQueue().cancelAll(TAG);
                Toast.makeText(getContext(), "Request failed"+TAG, Toast.LENGTH_SHORT).show();
            }
        }
    }, 10000);

i want to show a toast and cancel my request if there no response in 10 seconds how can i do this? Thank you in advance

  • 1
    instead of this i suggest you to define timeout..it will automatically fail the request after 10 second – Ashwini Saini Sep 23 '18 at 09:37
  • 1
    You might want to set a timeout for your request, see [https://stackoverflow.com/questions/17094718/change-volley-timeout-duration](https://stackoverflow.com/questions/17094718/change-volley-timeout-duration) – Macmist Sep 23 '18 at 09:48

1 Answers1

2

You can set timeout to your request.

after that particular timeout your request will be failed and you can catch that exception inside your onErrorResponse(VolleyError error) and here you can show Toast

in your case here is the example

 stringRequest.setRetryPolicy(new DefaultRetryPolicy(MY_TIMEOUT_MS,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
 MySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);

set MY_TIMEOUT_MS to 10000 milliseconds and DEFAULT_MAX_RETRIES to 0

now after 10 seconds the request will be failed and inside your ErrorResponse you can catch it like this and show Toast

@Override
public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

if (error instanceof TimeoutError || error instanceof NoConnectionError||error instanceof NetworkError) {
        Toast.makeText(context,context.getString(R.string.error_network_timeout),Toast.LENGTH_LONG).show();
         }
}
Ashwini Saini
  • 1,324
  • 11
  • 20
  • if the internet connection is off then it doesnt work. what can be donr in such condition?@SergeyGlotov – Mimoh Kulkarni Sep 23 '18 at 11:18
  • it doesnt trigger volley error when there is no internet connection – Mimoh Kulkarni Sep 23 '18 at 11:28
  • here you can get all error..it's a different question then what you asked https://stackoverflow.com/questions/21011279/android-volley-checking-internet-state – Ashwini Saini Sep 23 '18 at 11:33
  • if connection is off it not even triggering error. error.printStackTrace(); is also not working – Mimoh Kulkarni Sep 23 '18 at 11:44
  • please consider asking this as a separate problem, if your `onErrorResponse` not working on internet off then this problem is beyond what you asked..sometime the error can be null so check `if(error==null){System.out.println("error is null");}` – Ashwini Saini Sep 23 '18 at 11:57