Android Volley error.getMessage() is blank
Asked Answered
A

1

18

So I am making a POST JSonObjectRequest to my server and when it is successful everything works and the info is posted, but if there is an error and I try to display it in a toast, it shows up as blank. Here is my request:

private void logUserIn() {
    final String URL = Globals.BASE_URL +"/auth/login/";
    // Post params to be sent to the server
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("username", username.getText().toString());
    params.put("password", password.getText().toString());

    JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        Log.d("Log In User", response.toString());

                        //logged in db, changes screens
                        Intent nextScreen = new Intent(getApplicationContext(), MyProfile.class);
                        startActivity(nextScreen);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
            VolleyLog.e("Error: ", error.getMessage());
        }
    });

    // add the request object to the queue to be executed
    Globals.mRequestQueue.add(req);
}

error.getMessage() is always blank. Here is my response (tested using CURL) when the server returns an error:

{ "non_field_errors": [ "Unable to login with provided credentials." ] }

I can't seem to print this message. What am I missing? The POST works but the error response is showing blank...

Allusion answered 9/6, 2015 at 1:8 Comment(0)
I
46

The VolleyError object has a networkResponse reference, try to check it to see if you can get some useful information from there.

@Override
public void onErrorResponse(VolleyError error) {
    if (error == null || error.networkResponse == null) {
        return;
    }

    String body;
    //get status code here
    final String statusCode = String.valueOf(error.networkResponse.statusCode);
    //get response body and parse with appropriate encoding
    try {
        body = new String(error.networkResponse.data,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        // exception
    }

    //do stuff with the body...
}
Immunology answered 9/6, 2015 at 3:37 Comment(5)
Awesome! Had such a hard time doing this. Thanks for the help.Allusion
As a side note, you should put the statusCode line inside of the if statement because it will give null pointer if there is no response from server.Allusion
@AdityaVyas-Lakhan check mikebertiean's commentCypro
Careful what you do with body. Some 404 pages contain thousands of lines of code.Denson
Thank You! nice solution. Was looking for this from agesIngrown

© 2022 - 2024 — McMap. All rights reserved.