从服务器获取数据时使用Volley进行错误处理

时间:2019-05-15 06:05:32

标签: android api android-volley

我在我的项目中使用Volley来处理网络请求。这是我的服务器在有数据时返回的示例JSON,然后添加错误,否则给出错误

{
    "message_status": true,
    "data": [
        {
            "message_id": "88",
            "message_text": "hi,",
            "message_link": "0",
        },
}

{
    "message_status": false,
    "message": "Message not available!"
}

这是我的代码

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_msg,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                   try {
                            JSONObject jsonObject = new JSONObject(response);

                            if (jsonObject.has("data") && !jsonObject.isNull("data")) {

                                String success = jsonObject.getString("message_status");
                                String message = jsonObject.getString("message");

                                JSONArray jsonArray = jsonObject.getJSONArray("data");

                                if (success.equals("true")) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject object = jsonArray.getJSONObject(i);
                                        ChatMessage chatMessage = new ChatMessage();
                                        chatMessage.setMessageUser(object.getString("username"));
                                        chatMessage.setMessageTime(object.getString("time"));
                                        chatMessage.setMessageText(object.getString("message_text"));
                                        chatMessage.setUserId(object.getString("user_id"));
                                        chatMessage.setFileName(object.getString("file_name"));
                                        chatMessage.setMessageFile(object.getString("message_link"));
                                        chatMessage.setMessageID(object.getString("message_id"));
                                        chatMessages.add(chatMessage);
                                    }
                                    setupListview();
                                } else {
                                    // get message using error key
                                    String error = "Response : " + success + " = " + message;
                                    Toast.makeText(ChatActivity.this, error, Toast.LENGTH_SHORT).show();
                                }
                            }else {
                                Toast.makeText(ChatActivity.this, "data not available", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(ChatActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }

当数据没有值时,则不显示任何项目消息,但会显示服务器错误

1 个答案:

答案 0 :(得分:0)

尝试一下:

try {
    JSONObject jsonObject = new JSONObject(response);
    String success = jsonObject.getString("message_status");
    String message = jsonObject.getString("message");
    JSONArray jsonArray = jsonObject.getJSONArray("data");

    if (jsonArray != null || jsonArray.length() != 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setMessageUser(object.getString("username"));
            chatMessage.setMessageTime(object.getString("time"));
            chatMessage.setMessageText(object.getString("message_text"));
            chatMessage.setUserId(object.getString("user_id"));
            chatMessage.setFileName(object.getString("file_name"));
            chatMessage.setMessageFile(object.getString("message_link"));
            chatMessage.setMessageID(object.getString("message_id"));
            chatMessages.add(chatMessage);
            //loading.setVisibility(View.GONE);
        }
        setupListview();
    } else {
        // get message using error key
        Toast.makeText(ChatActivity.this, "error 1" + message + success, Toast.LENGTH_SHORT).show();
    }
} catch (Exception e) {
}