简体   繁体   English

排球的定制回应

[英]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. 当我发出请求并失败时,它显示带有完整响应错误的Toast,我只希望它发出“消息”,而不是整个JSON。

I want to make the message only appear and not the whole JSON String. 我想只显示消息,而不显示整个JSON字符串。

This is my JSON String: 这是我的JSON字符串:

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

This is the onResponse function: 这是onResponse函数:

        @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. 一种是如果您遇到某种网络问题,在这种情况下,不会调用onResponse() ,并且您没有可使用的任何JSON。

The other, shown in your code above, is if you have a JSON parsing error. 上面的代码中显示的另一个是是否存在JSON解析错误。 In that case, you cannot get the message, because the message is in the JSON, and you are unable to parse that JSON. 在这种情况下,您将无法获取消息,因为该消息位于JSON中,并且您无法解析该JSON。 What you are showing in the Toast is not valid JSON (as otherwise, you would not have gotten a JSONException ). Toast中显示的内容不是有效的JSON(否则,您将不会获得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. 然后, 查看Java堆栈跟踪以查看有关特定解析错误的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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