简体   繁体   中英

Custom response from Volley

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();
            }
        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM