如何在on Responde函数之外使用Retrofit responde?

问题描述 投票:0回答:0
public void doRequest(String food,final ApiCallback callback) {
        Call<FoodAPIResult> call = apiInterface.doGetFoodInfo(app_id, app_key, food);
        call.enqueue(new Callback<FoodAPIResult>() {
            @SuppressLint("LongLogTag")
            @Override
            public void onResponse(@NonNull Call<FoodAPIResult> call, @NonNull Response<FoodAPIResult> response) {
                foodInfo=new ArrayList<>();
                FoodAPIResult foodAPIResult = response.body();
                if (foodAPIResult != null) {
                    List<Hint> hints = new ArrayList<>(foodAPIResult.hints);
                    Log.d("FOOD_API", "----------------------------------------------------------------------------------------");
                    for (Hint list : hints) {
                        String label = "", brand = "", energyKcal = "", protein = "", fat = "", carbs = "", fibers = "";
                        label = list.food.label == null ? "" : list.food.label;
                        brand = list.food.brand == null ? "" : list.food.brand;
                        energyKcal = list.food.nutrients.enercKcal == null ? "" : String.valueOf(list.food.nutrients.enercKcal);
                        protein = list.food.nutrients.procnt == null ? "" : String.valueOf(list.food.nutrients.procnt);
                        fat = list.food.nutrients.fat == null ? "" : String.valueOf(list.food.nutrients.fat);
                        carbs = list.food.nutrients.chocdf == null ? "" : String.valueOf(list.food.nutrients.chocdf);
                        fibers = list.food.nutrients.fibtg == null ? "" : String.valueOf(list.food.nutrients.fibtg);
                        foodInfo.add(new FoodInfoModel(label, brand, energyKcal, protein, fat, carbs, fibers));
                    }
                }
                callback.onSuccess(foodInfo);
            }

            @Override
            public void onFailure(Call<FoodAPIResult> call, Throwable t) {

            }
        });

    }

嗨!我正在尝试在 onResponde 方法之外使用 foodInfo 列表。我已经复制了其他帖子中建议的回调,但这也会捕获 foodInfo 列表。 谢谢!

android retrofit2
© www.soinside.com 2019 - 2024. All rights reserved.