0

When i request to my api api returns 204 - No Content. But volley does not recognise that and give TimeOutError.

How can i handle this ?

Arda Kaplan
  • 1,720
  • 1
  • 15
  • 23

1 Answers1

1

When you setup a new volley request :

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
        // act upon a valid response
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        // Handle error
        }
    });

Notice that you pass a Response.ErrorListener. When error occurs, such as for instance 204, the onErrorResponse(VolleyError) callback is called with the VolleyError instance - error with appropriate information about the error passed to it.

So in this callback you should inspect for the error and take appropriate action.

new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if(error instanceof TimeoutError){
             // Take action when timeout happens
            }
        }
    }

NOTE : When timeout happens, the VolleyError instance is in fact an instance of TimeoutError a subclass of VolleyError. Hence we check if the error caused is timeout using instanceof

The list of VolleyError sub classes are available here : http://afzaln.com/volley/com/android/volley/VolleyError.html

The example given is for StringRequest type but the technique is the same for other VolleyObjectRequest types.

Karthiksrndrn
  • 188
  • 1
  • 12
  • First of all thanks for well-defined answer. I currently handle like this but this metodoly is wrong, i think. Because if it really occurs timeot connection, i will miss it. This logic makes this; timeout exception -> no content no content -> no content – Arda Kaplan May 18 '16 at 08:54
  • Oh, I understand now. `Volley` misinterprets 204 for `TimeoutError`. Silly me! I completely misunderstood it. – Karthiksrndrn May 18 '16 at 13:36
  • Possible duplicate. Check [this out](http://stackoverflow.com/questions/27392813/android-volley-library-not-working-with-204-and-empty-body-response) – Karthiksrndrn May 18 '16 at 13:39