簡體   English   中英

null值0 java.lang.String無法轉換為JSONObject [android]

[英]Value of null 0 java.lang.String cannot be converted to JSONObject [android]

我正在為餐廳的覆蓋范圍工作,如果所選餐廳不在覆蓋范圍內,則需要顯示消息。 如果在覆蓋區域中,則用戶可以繼續點餐。

只要餐廳不為空,則表示餐廳在覆蓋范圍內,應用運行良好。 如果沒有覆蓋范圍,則說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"))

應用檢查以查看餐廳是否為空

if (restaurant !=null && restaurants.length() > 0 && restaurants.getJSONObject(0) != null)


[英]Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject


[英]Value <br of type java.lang.String cannot be converted to JSONObject

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM