How to get error message description using Volley

how to handle volley error in android
volley timeout error
android volley exception
volley error in android studio
volley no connection error android
android volley multiple request queues
volley android
com android volley client error

I'm sending an http request from Android Java using the Volley library to a c# backend. The backend application responds with a error code and description as intended, as well as a StatusDescription. I can see the response status description through wireshark but do not know how to get the description string on the android side.

    final JsonObjectRequest request = new JsonObjectRequest(JsonObjectRequest.Method.POST,
                                url,json,
                            new Response.Listener<JSONObject>() {

                                @Override
                                public void onResponse(JSONObject response) {
                                    TextView mTextView = (TextView) findViewById(R.id.output);
                                    print("Success");
                                }
                            }, new Response.ErrorListener() {

                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    TextView mTextView = (TextView) findViewById(R.id.output);
                                    print("Failure (" + error.networkResponse.statusCode + ")");
//Trying to get the error description/response phrase here
                            }
                        }
                    );

This is the C# code processing the request:

[WebInvoke(Method = "POST", UriTemplate = "users", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] [OperationContract] void addUser(String username, String firstname, String lastname, String email, String hash) { Console.WriteLine(DateTime.Now + " Packet receieved");

        //Stores the response object that will be sent back to the android client
        OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
        String description = "User added";
        response.StatusCode = System.Net.HttpStatusCode.OK;

        //Tries to add the new user
        try
        {
            userTable.Insert(username,firstname,lastname,email,hash);
        }
        catch (SqlException e)
        {
            //Default response is a conflict
            response.StatusCode = System.Net.HttpStatusCode.Conflict;

            description = "Bad Request (" + e.Message + ")";

            //Check what the conflict is
            if (userTable.GetData().AsEnumerable().Any(row => username == row.Field<String>("username")))
            {
                description = "Username in use";
            }
            else if (userTable.GetData().AsEnumerable().Any(row => email == row.Field<String>("email")))
            {
                description = "Email address in use";
            }
            else
            {
                response.StatusCode = System.Net.HttpStatusCode.BadRequest;
            }
        }

        //display and respond with the description
        Console.WriteLine(description);
        response.StatusDescription = description;
    }

I've looked through other peoples questions but can't seem to find the answer I'm looking for. Anyone know how to do this? Many methods I have tried resulted in empty curly braces, indicating JSON with an empty body. I am trying specifically to get the status description.


Try with this custom method:

public void parseVolleyError(VolleyError error) {
        try {
            String responseBody = new String(error.networkResponse.data, "utf-8");
            JSONObject data = new JSONObject(responseBody);
            JSONArray errors = data.getJSONArray("errors");
            JSONObject jsonMessage = errors.getJSONObject(0);
            String message = jsonMessage.getString("message");
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
        } catch (JSONException e) {
        } catch (UnsupportedEncodingException errorr) {
        }
    }

It will show toast with error message from the request. Call this in onErrorResponse method in your volley request:

new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                           parseVolleyError(error);
                        }
                    }

Uses of Class com.android.volley.VolleyError, indicating that there was an authentication failure when performing a Request. If you are unfamiliar with Volley, please see Google I/O 2013 Video. Unfortunately, there is almost no documentation on Volley. So, I put together code snippets on how to make Volley HTTP Requests (GET, POST, PUT, DELETE). Setting up Android Volley Library. Setting up is straight-forward.


The data field of networkResponse is a JSON string of the form:

{"response":false,"msg":"Old Password is not correct."}

So you need to get the value corresponding to "msg" field, like this (ofcourse with all exception catching):

String responseBody = new String(error.networkResponse.data, "utf-8");
JSONObject data = new JSONObject(responseBody);
String message = data.optString("msg");

Tested with Volley 1.1.1

com.android.volley.ClientError java code examples, This page provides Java code examples for com.android.volley. @param err Volley error * @param context Context * @return String */ private static String new JSONObject(string); if (object.has("message")) { return object.get("message")​. ErrorListener Object: anonymous inner type , an implementaion of onErrorResponse(VolleyError err) will get an instance of object of Volley Error; In the onResponse() method, we are logging the output to LogCat and also showing the response recieved in an AlertDialog. In the onErrorResponse() method, we are simply logging the error message to LogCat.


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

Java Code Examples com.android.volley.VolleyError.getMessage, I have this Android app that does this app logic, User -> Reserves an Or you could have a particular error code like ITEM_ALREADY_EXISTS  This page provides Java code examples for com.android.volley.error.VolleyError. The examples are extracted from open source Java projects.


Example to override methods :

final JsonObjectRequest request = new JsonObjectRequest(JsonObjectRequest.Method.POST,
            url, json,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    TextView mTextView = (TextView) findViewById(R.id.output);
                    print("Success");
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            TextView mTextView = (TextView) findViewById(R.id.output);
            print("Failure (" + error.networkResponse.statusCode + ")");
        }
    }){
        @Override
        protected VolleyError parseNetworkError(VolleyError volleyError) {
            return super.parseNetworkError(volleyError);
        }

        @Override
        public void deliverError(VolleyError error) {
            super.deliverError(error);
        }
    };

Solved: Volley returns null error after hitting an endpoint that sends , getError(); Throwable cause;new VolleyError(cause); Request request;request. Android: How handle message error from the server using Volley? //In your  1- int method – which define your method type which can be GET, POST, PUT, DELETE. 2- String url – this is your url, where you want to send a request and get a response. 3- JSONObject jsonValue – this is your JSON data which will be used when you send the post request. 4- VolleyCallback callback – This is your interface object.


IMO, you should override parseNetworkError as below:

@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
    String json;
    if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
        try {
            json = new String(volleyError.networkResponse.data,
                    HttpHeaderParser.parseCharset(volleyError.networkResponse.headers));
        } catch (UnsupportedEncodingException e) {
            return new VolleyError(e.getMessage());
        }
        return new VolleyError(json);
    }
    return volleyError;
}

Then, inside onErrorResponse(VolleyError error), you can use Log.e(LOG_TAG, error.toString()); for example. Hope it helps!

com.android.volley.VolleyError java code examples, public class VolleyErrorHelper { /** * Returns appropriate message which is to be displayed to the user * against the specified error object. * * @param error * @​param context * @return  Volley Uses Caches To Improve Performance In Android: Volley uses caches concept to improve the performance of App. For example, lets say you are using Asynctask to fetch image and description from a JSON array created in server API.


Volley, Callback method that an error has been occurred with the provided error code and optional user-readable message. protected VolleyError, Request. In SQL Server, Every error is recognized with a specific error message and this SQL Error Message function will help us to fetch that error message


How handle message error from the server using Volley , I am using Volley for my Android app to fetch data from my server. Here's some example code to get you started: public void onErrorResponse(VolleyError error) response but its not able to get response data which is in JSON format Status  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.


Make a standard request, Volley provides the following classes for JSON requests: JsonArrayRequest —A request for retrieving a JSONArray response body at a given  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.