我正在为餐馆的Coverage区域工作,如果所选餐厅不在覆盖范围内,我需要显示消息。如果它在覆盖区域中,则用户可以继续订购膳食。
如果餐厅不为空,则意味着如果餐厅在覆盖范围内,则应用程序运行正常。如果没有覆盖,请说JSON数组为null,则上面提到的错误显示在catch块中。
以下是代码:
`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)
{
这一行。
答案 0 :(得分:1)
这是我所做的唯一改变。现在它工作得很好。
if (!restaurants.getString(0).equals("null"))
答案 1 :(得分:0)
应用支票查看餐厅是否为空
if (restaurant !=null && restaurants.length() > 0 && restaurants.getJSONObject(0) != null)