3

I have implemented function to get the restaurant area minimum order. In check_minimum_order() function, I have got desired result from the response. The value is rest_area_min_order = 10. Now, I want to pass the value which I received through JSON to the next function So that I can do calculation part.

Here is the code of check_minimum_order()

    private void check_minimum_order(String restaurant_id)
{
    try
    {
        String url;
        if(appPrefs.getLanguage().equalsIgnoreCase("ar"))
            url = LinksConstants.BASE_URL
                    + LinksConstants.CHECK_MINIMUM_ORDER;
        else
            url = LinksConstants.BASE_URL
                    + LinksConstants.CHECK_MINIMUM_ORDER;

        RequestParams params = new RequestParams();

        params.put("restaurant_id", restaurant_id);
        params.put("area_id", 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);

                try
                {
                    if (response != null)
                    {
                        rest_area_min_order = response.getString("restaurant_area_min_order");
                    }
                }
                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();
    }
}

Now, the above check_minimum_order() function gave me the value of rest_area_min_order as 10. Now, I want to use this rest_area_min_order in another function. Here is the code: `

            check_minimum_order(restaurant_id);

            HashMap<String, Object> items_hash = (HashMap<String, Object>) rest_cart_hash.get(restaurant.getId());

            items_hash.put("delivery_pickup_time", time);
            items_hash.put("pickup_address_id", pickup_id);
            items_hash.put("payment_method_id", payment_id);
            items_hash.put("delivery_pickup", delivery_pickup);
            items_hash.put("selected_user_address_id", user_address_id);
            items_hash.put("rest_area_min_order", rest_area_min_order);

            restaurantList.add(items_hash);

            String rest_min_order = (String) items_hash.get("rest_min_order");
            String rest_subtotal = (String) items_hash.get("rest_subtotal");
            String rest_area_min_order = (String) items_hash.get("rest_area_min_order");

            boolean isError = isValidMinOrderAmount(rest_min_order, rest_subtotal, rest_area_min_order);`
Jacky
  • 298
  • 3
  • 15

4 Answers4

0

Basically your onSuccess function return void so you cannot return anything. You can simply call another function in onSuccess (For example setMinimumOrder(int pMinimumOrder)) which will take rest_area_min_order as a input and you can perform rest of the things as per your requirement.

Akshay
  • 2,506
  • 4
  • 34
  • 55
0

As Far as I know, there are two options to achieve this.

1) Put the code to be executed, directly in onSuccess after getting the value of rest_area_min_order. Or you can create a separate method for that.

2) Using Interface to pass value.

I prefer the first option which is very simple.

Ganesh S
  • 31
  • 2
  • 6
0

From your question, I understand that you have to do some calculation after getting web service response. If the rest of calculation needs to done in same class then call that method after getting response

if (response != null)
{
  rest_area_min_order = response.getString("restaurant_area_min_order");
   doPostCalculations(rest_area_min_order);
}

If check_minimum_order() method is in Network class and you are calling this method from another class (eg: activity), then you can communicate back to activity(or the class that called the method) using interface on getting the response.

Here is a sample interface that you can use.

public interface ApiListener {
   void onSuccess(JSONObject response);
   void onFailure();
}

Please have a look at this post for getting more idea about interface - How to create our own Listener interface in android?

Suneesh Ambatt
  • 1,347
  • 1
  • 11
  • 41
0

The main reason is that in your check_minimum_order method you post your networking to another thread if you wan to do some work with the result returned from network then you must call it in on success after the result is really fetched.

Sahar
  • 301
  • 1
  • 6