I am using Volley for my Android app to fetch data from my server. It works well except when handling the error from my server. My server sends this response when there is a mistake:
{ "status": 400, "message": "Errors (2): A name is required- Julien is already used. Not creating." }
My goal is to get the message and then display it in a Toast
. I followed some sample for how to do this, but it doesn't work.
There is my error listener :
public void onErrorResponse(VolleyError error) { int statusCode = error.networkResponse.statusCode; NetworkResponse response = error.networkResponse; Log.d("testerror",""+statusCode+" "+response.data); // Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button. // For AuthFailure, you can re login with user credentials. // For ClientError, 400 & 401, Errors happening on client side when sending api request. // In this case you can check how client is forming the api and debug accordingly. // For ServerError 5xx, you can do retry or handle accordingly. if( error instanceof NetworkError) { } else if( error instanceof ClientError) { } else if( error instanceof ServerError) { } else if( error instanceof AuthFailureError) { } else if( error instanceof ParseError) { } else if( error instanceof NoConnectionError) { } else if( error instanceof TimeoutError) { } showProgress(false); mPasswordView.setError(getString(R.string.error_incorrect_password)); mPasswordView.requestFocus(); }
And there the result of my debugger : testerror﹕ 400 [B@430b8d60
EDIT: Moreover my error.getMessage() is null.
So I don't understand why my variable response.data is not the response from my server.
If someone know how I can get the message from my server it's will be cool.
Thx,
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.
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.
It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again. Volley is not suitable for large download or streaming operations since Volley holds all responses in memory during parsing.
Volley Uses Caches To Improve Performance In Android: For example, lets say you are using Asynctask to fetch image and description from a JSON array created in server API. The content is fetched in the portrait mode and now user rotate screen to change it to landscape mode.
I've implemented something similar to this, and it's relatively simple. Your log message is printing out what looks like gibberish, because response.data
is really a byte array - not a String
. Also, a VolleyError
is really just an extended Exception
, so Exception.getMessage() likely wouldn't return what you are looking for unless you override the parsing methods for parsing your VolleyError
in your extended Request
class. A really basic way to handle this would be to do something like:
//In your extended request class @Override protected VolleyError parseNetworkError(VolleyError volleyError){ if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){ VolleyError error = new VolleyError(new String(volleyError.networkResponse.data)); volleyError = error; } return volleyError; } }
If you add this to your extended Request
classes, your getMessage()
should at least not return null. I normally don't really bother with this, though, since it's easy enough to do it all from within your onErrorResponse(VolleyError e)
method.
You should use a JSON library to simplify things -- I use Gson for example or you could use Apache's JSONObject
s which shouldn't require an additional external library. The first step is to get the response JSON sent from your server as a String
(in a similar fashion to what I just demonstrated), next you can optionally convert it to a JSONObject (using either apache's JSONObject
s and JsonArray
s, or another library of your choice) or just parse the String
yourself. After that, you just have to display the Toast
.
Here's some example code to get you started:
public void onErrorResponse(VolleyError error) { String json = null; NetworkResponse response = error.networkResponse; if(response != null && response.data != null){ switch(response.statusCode){ case 400: json = new String(response.data); json = trimMessage(json, "message"); if(json != null) displayMessage(json); break; } //Additional cases } } public String trimMessage(String json, String key){ String trimmedString = null; try{ JSONObject obj = new JSONObject(json); trimmedString = obj.getString(key); } catch(JSONException e){ e.printStackTrace(); return null; } return trimmedString; } //Somewhere that has access to a context public void displayMessage(String toastString){ Toast.makeText(context, toastString, Toast.LENGTH_LONG).show(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With