【问题标题】:How to show a toast if no item us present in the JSON?如果 JSON 中不存在任何项目,如何显示敬酒?
【发布时间】:2019-09-05 07:28:26
【问题描述】:

当 JSON 为空时显示此错误:org.json.JSONException: answerList 没有价值

{
        "answerList": [
            {
                "id": 117,
                "edu_forum_id": 167,
                "edu_forum_answer_id": 0,
                "answer": "cvf",
                "attachment": "https://testapi.ourschoolzone.org/uploads/upload_eafd8dec-8237-43f0-ae83-7ad05df6209f_cropped6342785145197935935.jpg",
                "answerdate": "31/08/2019 04:32 PM",
                "school_id": "142388",
                "school_name": "Demo-Irvine High School",
                "school_address": "Panbazar, Guwahati, Assam",
                "school_logo": "http://test.ourschoolzone.org/Upload/9873c7ee-1542-4b42-ba17-771d0b99f26f.png",
                "parent_id": "3916",
                "parent_name": "Intiaz Khan",
                "parent_profile_image": "https://admin.ourschoolzone.org/Images/male_icon.png",
                "teacher_id": "",
                "teacher_name": "",
                "teacher_profile_image": "https://test.ourschoolzone.org/Upload/a0ae18da-940b-47ee-86b1-b159318a0cd6.jpg"
            },

这是使用 Volley 的 StringRequest。我正在获取 ListView 中的所有详细信息

 try {
                        //getting the whole json object from the response
                        JSONObject obj = new JSONObject(response);
                        Log.e("ResponSe",response);



                        //we have the array named hero inside the object
                        //so here we are getting that json array
                        JSONArray heroArray = obj.getJSONArray("answerList");


                        //now looping through all the elements of the json array
                        for (int i = 0; i < heroArray.length(); i++) {

                            //getting the json object of the particular index inside the array
                            JSONObject heroObject = heroArray.getJSONObject(i);



                            //creating a hero object and giving them the values from json object
                            ViewAnswersModel hero = new ViewAnswersModel(heroObject.getString("answer"),
                                    heroObject.getString("answerdate"),
                                    heroObject.getString("school_name"),
                                    heroObject.getString("school_logo"),
                                    heroObject.getString("attachment"));






                            //adding the hero to herolist
                            heroList.add(hero);
                        }

**

如何显示未找到条目的 toast !

**

【问题讨论】:

  • if(answerlist.size()==0) Toast.show();
  • 这对我不起作用!

标签: android json android-toast


【解决方案1】:

使用 try catch 处理异常并在 catch 块内执行 Toast。

try {
  // attempt to parse json
} catch(JSONException exception) {
  Toast.makeText(this, "No response from server", Toast.LENGTH_LONG).show();
}  

【讨论】:

  • 不客气,您可以将答案标记为正确并点赞,谢谢。
  • @JakeB 请不要要求人们支持您的答案。如果它好的话,社区会支持它。
【解决方案2】:

这样做:-

            JSONObject parentObject = new JSONObject(response);

            JSONArray jsonArray = parentObject.getJSONArray("answerList");
            if (jsonArray.length() == 0) {
                // No Data
            }else{
               //have data
               }

【讨论】:

  • 长度或尺寸()?
  • 大小用于arrayList,长度用于jsonArray
猜你喜欢
  • 2016-11-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 2018-01-06
  • 1970-01-01
相关资源
最近更新 更多