简体   繁体   English

如何从使用json响应的函数返回值?

[英]How can I return a value from a function that uses json response?

I have implemented function to get the restaurant area minimum order. 我已经实现了功能,以获得餐厅区域的最低订单。 In check_minimum_order() function, I have got desired result from the response. 在check_minimum_order()函数中,我得到了响应所需的结果。 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. 值为rest_area_min_order = 10.现在,我想将通过JSON接收的值传递给下一个函数,以便我可以进行计算部分。

Here is the code of check_minimum_order() 这是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. 现在,上面的check_minimum_order()函数给了我rest_area_min_order的值为10.现在,我想在另一个函数中使用这个rest_area_min_order。 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);`

Basically your onSuccess function return void so you cannot return anything. 基本上你的onSuccess函数返回void所以你不能返回任何东西。 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. 您可以简单地调用onSuccess另一个函数(例如setMinimumOrder(int pMinimumOrder) ),它将rest_area_min_order作为输入,您可以根据您的要求执行其余的操作。

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 . 1)获取rest_area_min_order的值后,直接在onSuccess中放入要执行的代码。 Or you can create a separate method for that. 或者您可以为此创建单独的方法。

2) Using Interface to pass value. 2)使用Interface传递值。

I prefer the first option which is very simple. 我更喜欢第一个非常简单的选项。

From your question, I understand that you have to do some calculation after getting web service response. 从您的问题中,我了解到在获得Web服务响应后您必须进行一些计算。 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. 如果check_minimum_order()方法在Network类中,并且您从另一个类(例如:activity)调用此方法,那么您可以使用接口在获取响应时与活动(或调用该方法的类)进行通信。

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? 请看一下这篇文章以获得更多关于界面的想法 - 如何在android中创建我们自己的Listener界面?

主要原因是在check_minimum_order方法中,如果您想对从网络返回的结果进行一些工作,那么您将网络发布到另一个线程,那么您必须在真正获取结果后成功调用它。

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

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