Android 如果JSON中没有任何项目,如何向我们敬酒?
当JSON为空时,显示以下错误:org.JSON.JSONException: 回答列表没有值 下面是使用截击的StringRequest。我正在获取ListView中的所有详细信息Android 如果JSON中没有任何项目,如何向我们敬酒?,android,json,android-toast,Android,Json,Android Toast,当JSON为空时,显示以下错误:org.JSON.JSONException: 回答列表没有值 下面是使用截击的StringRequest。我正在获取ListView中的所有详细信息 try { //getting the whole json object from the response JSONObject obj = new JSONObject(response);
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);
}
**
如何显示未找到条目的祝酒词
**使用try catch处理异常,并在catch块中预成型Toast 请这样做:
JSONObject parentObject = new JSONObject(response);
JSONArray jsonArray = parentObject.getJSONArray("answerList");
if (jsonArray.length() == 0) {
// No Data
}else{
//have data
}
ifanswerlist.size==0.show;这对我不管用!长度还是大小?大小代表arrayList,长度代表jsonArrayYou’不客气,您可以标记答案正确并向上投票谢谢。@JakeB请不要;Don’不要要求别人对你的答案投赞成票。如果它是好的,社区将投票支持它。
try {
// attempt to parse json
} catch(JSONException exception) {
Toast.makeText(this, "No response from server", Toast.LENGTH_LONG).show();
}
JSONObject parentObject = new JSONObject(response);
JSONArray jsonArray = parentObject.getJSONArray("answerList");
if (jsonArray.length() == 0) {
// No Data
}else{
//have data
}