用户选择区域后,有一个名为checkCoverageGuest()的功能。此功能检查用户选择的区域是否在该餐厅的覆盖范围内。如果餐厅不在承保范围内,则checkCoverageGuest()将返回真实值。如果该餐厅没有覆盖范围,则checkCoverageGuest()将返回false。

如果该值为true,那么它将继续进行下一级结帐。如果值为false,屏幕将显示烤面包错误消息,表明所选餐厅不在承保范围内。

现在,所有从此函数checkCoverageGuest()发出的结果均为false。

这是我的代码。

private boolean checkCoverageGuest() {
    try {
        String url;
        if (appPrefs.getLanguage().equalsIgnoreCase("ar"))
            url = LinksConstants.TEST_SERVER_URL
                    + LinksConstants.SEARCH_COVERAGE_AREA;
        else
            url = LinksConstants.TEST_SERVER_URL
                    + LinksConstants.SEARCH_COVERAGE_AREA;

        Intent startingIntent = getIntent();
        city_id = getIntent().getStringExtra("id");

        RequestParams params = new RequestParams();
        params.put("area", city_id);

        NetworkRestClient.post(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                progressActivity.showLoading();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progressActivity.showContent();
                JSONObject[] restaurants_arr = null;

                hasCoverageGuest = true;
                try {
                    if (response != null) {

                        JSONArray restaurants = response.getJSONArray("restaurants");

                        // if restaurants deliver to that area
                        if (!restaurants.getString(0).equals("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();
                                    hasCoverageGuest = false;
                                    break;
                                }

                            }
                            //end of outer for
                        } //end of if

                        else //if restaurants don't deliver to that area
                        {
                            hasCoverageGuest = false;
                        }

                    } // end of if response != null


                } // end of try

                catch (Exception ex) {
                    GSLogger.e(ex);
                    showError();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String errorResponse, Throwable throwable) {
                super.onFailure(statusCode, headers, errorResponse, throwable);

                showError();

                if (AppConstants.DEBUG_MODE)
                    showToast(errorResponse);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);

                showError();
            }
        });

    } catch (Exception ex) {
        GSLogger.e(ex);
        showError();
    }
    return hasCoverageGuest;
}

最佳答案

您的private boolean checkCoverageGuest()方法在执行public void onSuccess()之前返回,这就是checkCoverageGuest()始终返回false的原因。

如果在获得hasCoverageGuest好的值后需要执行某些操作,请在onSucess()内部执行

10-04 12:05