始终将false返回为布尔值

时间:2017-07-24 02:19:48

标签: java android

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

如果值为true,则进入下一级结账。如果值为false,则屏幕将显示所选餐厅未覆盖的吐司错误消息。

现在,每个这个函数checkCoverageGuest()都是假的。

这是我的代码。

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;
}

2 个答案:

答案 0 :(得分:1)

您需要使用回调来指定成功或失败。一种方法是使用界面。

public interface notifyGuestCoverage {
      void hasCoverage(int status);
}
在onSuccess中

if (!restaurants.getString(0).equals("null")) {
     notifyGuestCoverageObj.hasCoverage(true);
}

此接口将由需要覆盖值的类实现。对于前,

class Example implements notifyGuestCoverage{
    void hasCoverage(int status){
            //your code
    }
}

答案 1 :(得分:0)

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

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

内执行