Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the contents of an error response in Volley?

Using any of these examples: http://developer.android.com/training/volley/request.html

I understand how to process the response of a succesful request, and how to detect and react to an error.

However, an error may be (among other situations) a 40x or 50x response from the server, in which case the response can still contain data (headers and body).

But the error listener is only passed a VolleyError object (which is a subclass of Exception if I'm not mistaken) and not a Response object.

How do I access the content of an error response?

like image 938
matteo Avatar asked Oct 02 '14 18:10

matteo


People also ask

How do I get volley error message?

you have to override parseNetworkError and deliverError methods and you can get errormessage from them.

What is volley client error?

Indicates that the server responded with an error response indicating that the client has erred. For backwards compatibility, extends ServerError which used to be thrown for all server errors, including 4xx error codes indicating a client error.

How do I use volley API?

Android App Development for Beginners This example demonstrate about How to use simple volley request in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


Video Answer


2 Answers

The VolleyError object has a networkResponse reference, which itself has a 'data' member, which is a byte array of the response body. If you want to view the data in the case of an error code in response, you can use something like this:

@Override public void onErrorResponse(VolleyError error) {     String body;     //get status code here     String statusCode = String.valueOf(error.networkResponse.statusCode);     //get response body and parse with appropriate encoding     if(error.networkResponse.data!=null) {         try {             body = new String(error.networkResponse.data,"UTF-8");         } catch (UnsupportedEncodingException e) {             e.printStackTrace();         }     }     //do stuff with the body... } 
like image 191
bozzle Avatar answered Sep 23 '22 01:09

bozzle


In StringRequest for example:

@Override protected Response<String> parseNetworkResponse(NetworkResponse response) {     Map<String, String> responseHeaders = response.headers;     if (response.statusCode == 401) {         // Here we are, we got a 401 response and we want to do something with some header field; in this example we return the "Content-Length" field of the header as a successfully response to the Response.Listener<String>         Response<String> result = Response.success(responseHeaders.get("Content-Length"), HttpHeaderParser.parseCacheHeaders(response));         return result;     } // else any other code that carries a message     return super.parseNetworkResponse(response); } 
like image 20
ADD Avatar answered Sep 25 '22 01:09

ADD