Arda Kaplan
Arda Kaplan

Reputation: 1758

How to detect 204 No Content from Android Volley Request

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 ?

Upvotes: 0

Views: 1766

Answers (1)

Karthiksrndrn
Karthiksrndrn

Reputation: 178

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.

Upvotes: 1

Related Questions