How can I return a value from a function using a json response?
I implemented a function to get the minimum order in the restaurant. In the check_minimum_order () function, I have the desired response result. Rest_area_min_order value = 10. Now I want to pass the value I got via JSON to the next function. This way I can do the calculation part.
Here is the check_minimum_order () code
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 this check_minimum_order () function gave me the rest_area_min_order value 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);`
source to share
As far as I know, there are two options for this.
1) Place the code to be executed directly in onSuccess after getting the rest_area_min_order value . Or you can create a separate method for this.
2) Using an interface to transfer a value.
I prefer the first option, which is very simple.
source to share
From your question, I understand that you need to do some calculations after receiving the response from the web service. If the rest of the calculation needs to be done in the same class, then call this method after receiving the response
if (response != null)
{
rest_area_min_order = response.getString("restaurant_area_min_order");
doPostCalculations(rest_area_min_order);
}
If the check_minimum_order () method is in the Network class and you are calling this method from another class (ex: activity), then you can return to the activity (or a class called a method) using the interface to get the response.
Here is an example of an interface you can use.
public interface ApiListener {
void onSuccess(JSONObject response);
void onFailure();
}
Please take a look at this post for more interface information - How to create custom Listener interface in android?
source to share