简体   繁体   English

null值0 java.lang.String无法转换为JSONObject [android]

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

I am working on Coverage area for restaurants where I need to display message if the selected restaurant is not in coverage area. 我正在为餐厅的覆盖范围工作,如果所选餐厅不在覆盖范围内,则需要显示消息。 If it is in coverage area, the user can proceed to order the meal. 如果在覆盖区域中,则用户可以继续点餐。

As far as there is restaurant is not null means if the restaurant is in coverage area, the app is running perfectly. 只要餐厅不为空,则表示餐厅在覆盖范围内,应用运行良好。 When there is no coverage, say the JSON Array is null then the above mentioned error is displaying in catch block. 如果没有覆盖范围,则说JSON数组为空,则上述错误将显示在catch块中。

Here is the code: 这是代码:

`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();
                    }
                }

` `

When i set debug point, It not allowing me to get inside of if (restaurants.length() > 0 && restaurants.getJSONObject(0) != null) { this line. 当我设置调试点时,它不允许我进入if (restaurants.length() > 0 && restaurants.getJSONObject(0) != null) {这行。

This is the Only change I made. 这是我所做的唯一更改。 Now It is working perfectly. 现在,它运行良好。 if (!restaurants.getString(0).equals("null"))

应用检查以查看餐厅是否为空

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM