0

When i'm making a request and it fails, it shows the Toast with a full response error, I just want the "message" from it and not the whole JSON.

I want to make the message only appear and not the whole JSON String.

This is my JSON String:

{"status":false,"message":"message here"}

This is the onResponse function:

        @Override
        public void onResponse(String response){
            try {
                JSONObject volleyResponse = new JSONObject(response);

                boolean success = volleyResponse.getBoolean("success");
                final String message = volleyResponse.getString("message");

                if(success){
                    messageText.setText("success");
                }
            } catch(JSONException e) {
                Toast.makeText(Authentication.this, response, Toast.LENGTH_LONG).show();
            }
        }

1 Answers1

0

By definition, that is not possible.

There are two main failure cases. One is if you have some sort of networking problem, in which case onResponse() is not called, and you do not have any JSON to work with.

The other, shown in your code above, is if you have a JSON parsing error. In that case, you cannot get the message, because the message is in the JSON, and you are unable to parse that JSON. What you are showing in the Toast is not valid JSON (as otherwise, you would not have gotten a JSONException).

In your exception handler, add:

Log.e("heyoooooo", "Exception parsing JSON", e);

Then, look at the Java stack trace to see more details about the particular parsing error.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I should remove the "success" string and leave the message itself and display it to a TextView? –  Jun 04 '17 at 16:54
  • There isn't actually much of an error, it's more like a "authentication" problem, like invalid username, and password –  Jun 04 '17 at 16:55
  • @heyoooooo: Your two comments do not seem to be related to this question. Your question is about what is shown in the `Toast` ("it shows the Toast with a full response error, I just want the "message" from it and not the whole JSON"). You are showing the `Toast` in response to a `JSONException`, based on the code shown in your question. – CommonsWare Jun 04 '17 at 16:57