我正在为餐厅的覆盖范围工作,如果所选餐厅不在覆盖范围内,则需要显示消息。如果在覆盖范围内,则用户可以继续点餐。

就餐厅而言,如果餐厅不为空,则表示餐厅位于覆盖范围内,则该应用程序运行良好。如果没有覆盖,则说JSON数组为空,则上述错误将显示在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) {
这条线。

最佳答案

这是我所做的唯一更改。现在,它运行良好。
 if (!restaurants.getString(0).equals("null"))

07-26 09:30