Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sometimes this method gives the Required result, But sometimes it gives either calorie null or Whole list Blank. #8

Open
dhiraj07tiwari opened this issue Jun 12, 2018 · 0 comments

Comments

@dhiraj07tiwari
Copy link

dhiraj07tiwari commented Jun 12, 2018

Hii,
I am using Fatsecret libraries for Search food with calories and servings of the food.
Here my code is:
public CustomResponse searchFoodItems(String query,Integer page) {
CustomResponse customResponse=null;
Response response = service.searchFoods(query,page);
//This response contains the list of food items at zeroth page for your query
SearchFoodDto searchFoodDto = new SearchFoodDto();
List searchFoods=new ArrayList();
try {
searchFoods = new ArrayList();
if(null!= response) {
int pageSize = response.getTotalResults()/response.getMaxResults();
searchFoodDto.setTotalPages(pageSize);
List compactFoods = response.getResults();
for (CompactFood compactFood :compactFoods) {
SearchFoodListDto searchFood=new SearchFoodListDto(); searchFood.setFoodName(compactFood.getName()); searchFood.setId(compactFood.getId()); searchFood.setType(compactFood.getType()); searchFoods.add(getFoodCalories(searchFood));
}
}
} catch (Exception e) {
e.printStackTrace();
}
searchFoodDto.setSearchFoodList(searchFoods);
customResponse=new CustomResponse<>(searchFoodDto,Status.SUCCESS);
return customResponse;
}
SearchFoodListDto getFoodCalories(SearchFoodListDto searchFoodListDto) {

	BigDecimal calories = BigDecimal.valueOf(0.0);
    try {
        Food food = service.getFood(searchFoodListDto.getId());
        if (null != food) {
        	for (Serving serving : food.getServings()) {     	  searchFoodListDto.setCalories(serving.getCalories());       		searchFoodListDto.setServing(Calculations.round(Double.valueOf(serving.getNumberOfUnits().toString()), 0)+serving.getMeasurementDescription());
        	}
        }
        else{
            searchFoodListDto.setCalories(calories);
        }
            
    } catch (Exception e) {
        e.printStackTrace();
    }
	return searchFoodListDto;
}

Sometimes this method gives the Required result.
But sometimes it gives either calorie null or Whole list Blank.

I am unable to fulfill my requirement.
Thanks,
Dhiraj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant