fatsecret/fatsecret4j

How to get servings?

ariefpurnamamuharram opened this issue · 12 comments

Hello,
Can you help me how to get serving details?
Thank you.

Anyone can help me, please?

@ariefpurnamamuharram Recipe has Serving object in it from which you can get all the details. Also Food has an array of Service objects which give details about all kinds of different servings.

May I know which Recipe/Food are you trying to retrieve?

No, it is not about recipe.
Let me assume Indomie. I want to get the serving details of Indomie, just like Calories, Natrium, etc. How to do that? Can you give me an example code?
Thank you @ranesr

In that case, you are looking for Food.
You can use this code:

String key = "ENTER YOUR KEY HERE";
String secret = "ENTER YOUR SECRET HERE";
FatsecretService service = new FatsecretService(key, secret);
String query = "Indomie";
Response<CompactFood> response = service.searchFoods(query);
List<CompactFood> results = response.getResults();
for (CompactFood food: results) {
    Food f = service.getFood(food.getId());
    if (f.getServings() != null && f.getServings().size() > 0) {
        // The following line will give you Serving object.
        Serving serving = f.getServings().get(0);
        System.out.println("servingDescription: " + serving.getServingDescription());
        System.out.println("servingUrl: " + serving.getServingUrl());
        System.out.println("calories: " + serving.getCalories());
    }
}

You can find more about Serving object here. There is a lot of information you will get from this object, like calories, carbohydrates, proteins, etc.

I try to implement the code, but I encountered some errors. What should I do? @ranesr
Thank you.

screenshot from 2018-06-11 13-36-09

@ranesr can you help me by giving me the full codes of the example?
Thank you

@ariefpurnamamuharram Have you added this to your build.gradle file?

Yes, I have. But it doesn't resolve my problem. @ranesr

Any solution for my problem, please? @ranesr
Thank you

@ariefpurnamamuharram Can you share your FoodSearchActivity.java file?

@ranesr
I just copy the code that you have give it to me.

screenshot from 2018-07-12 02-40-32

@ranesr @ohdihe could anyone please help me?
thank you