我正在研究餐厅的覆盖区域,如果所选餐厅不在覆盖区域内,我需要显示消息。如果在覆盖范围内,用户可以继续订餐。
只要有餐厅不为空,则意味着如果餐厅在覆盖范围内,则应用程序运行正常。当没有覆盖时,假设 JSON 数组为 null,则上述错误将显示在 catch block 中。
代码如下:
`public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
progressActivity.showContent();
JSONObject[] restaurants_arr = null;
hasCoverage = true;
try {
if (response != null)
{
JSONArray restaurants = response.getJSONArray("restaurants");
// if restaurants deliver to that area
if (restaurants.length() > 0 && restaurants.getJSONObject(0) != null)
{
restaurants_arr = new JSONObject[restaurants.length()];
int has_coverage = 1; // assume coverage is there..
for (int j = 0; j < Utils.cart_restaurants.size(); j++)
{
RestaurantModel restaurant = Utils.cart_restaurants.get(j);
String restaurantCartID = restaurant.getId();
boolean found = false;
for (int i = 0; i < restaurants.length(); i++) {
restaurants_arr[i] = restaurants.getJSONObject(i);
String restaurantID = restaurants_arr[i].get("restaurant_id").toString();
if (restaurantCartID.equals(restaurantID))
{
found = true;
break;
}
} //end of inner for
if (found == false)
{
Toast.makeText(CheckoutActivity.this, "There is no coverage for the Selected Area ", Toast.LENGTH_SHORT).show();
hasCoverage = false;
break;
}
}
//end of outer for
} //end of if
else //if restaurants don't deliver to that area
{
hasCoverage = false;
}
} // end of if response != null
if(hasCoverage)
{
processOrder();
}
else
{
Toast.makeText(CheckoutActivity.this, "There is no coverage for the Selected Area ", Toast.LENGTH_SHORT).show();
}
} // end of try
catch (Exception ex) {
GSLogger.e(ex);
showError();
}
}
`
当我设置调试点时,它不允许我进入 if (restaurants.length() > 0 && restaurants.getJSONObject(0) != null)
{
这一行。
最佳答案
这是我所做的唯一更改。现在它工作得很好。
if (!restaurants.getString(0).equals("null"))
关于java - null 0 java.lang.String 的值无法转换为 JSONObject [android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45263404/