fatsecret4j

You can use this java/android client for consuming food.get(), foods.search(), recipe.get(), and recipes.search() methods provided by Fatsecret REST API

Android Documentation

Want your android app to make calls to fatsecret rest api? fatsecret4j is a Java library for Android that enables android devices to communicate with fatsecret api.

Install the Android SDK

To install the latest Client SDK library add the following configuration to your build.gradle file:

							

repositories {
    mavenCentral()
}

android {
	defaultConfig {
		minSdkVersion 24
		jackOptions {
			enabled true
		}
	}
	compileOptions {
		sourceCompatibility JavaVersion.VERSION_1_8
		targetCompatibility JavaVersion.VERSION_1_8
	}	
}

dependencies {
	compile 'com.fatsecret4j:fatsecret-platform:2.0'
	compile 'com.android.volley:volley:1.0.0'
}
							
						
Enable Internet Permissions

In order to target Android API level 24 or later, you will need to ensure that your application requests runtime permissions for internet access. To do this, perform the following step:

Add the following to your Android Manifest file:

							
<uses-permission android:name="android.permission.INTERNET"/>
							
						
Client Side Code

You will need to initialize Request with your Fatsecret Application Consumer Key and associated Consumer Secret and a listener which takes care of all the data; and you are good to search food items; get any particular food item; search recipe items; or get any particular recipe item.

Example

							
package com.yourpackage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import com.fatsecret.platform.model.CompactFood;
import com.fatsecret.platform.model.CompactRecipe;
import com.fatsecret.platform.model.Food;
import com.fatsecret.platform.model.Recipe;
import com.fatsecret.platform.services.Response;
import com.fatsecret.platform.services.android.Request;
import com.fatsecret.platform.services.android.ResponseListener;

import java.util.List;
...
public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		String key = "Replace this by your Application Consumer Key";
		String secret = "Replace this by your Consumer Secret";
		String query = "pasta";
		RequestQueue requestQueue = Volley.newRequestQueue(this);
		Listener listener = new Listener();
		
		Request req = new Request(key, secret, listener);
		
		//This response contains the list of food items at zeroth page for your query
		req.getFoods(requestQueue, query);
		
		//This response contains the list of food items at page number 3 for your query
		//If total results are less, then this response will have empty list of the food items
		req.getFoods(requestQueue, query, 3);
		
		//This food object contains detailed information about the food item
		req.getFood(requestQueue, 29304L);
		
		//This response contains the list of recipe items at zeroth page for your query
		req.getRecipes(requestQueue, query);
		
		//This response contains the list of recipe items at page number 2 for your query
		//If total results are less, then this response will have empty list of the recipe items
		req.getRecipes(requestQueue, query, 2);
		
		//This recipe object contains detailed information about the recipe item
		req.getRecipe(requestQueue, 315L);
	}
	
	class Listener implements ResponseListener {
		@Override
		public void onFoodListRespone(Response<CompactFood> response) {
			System.out.println("TOTAL FOOD ITEMS: " + response.getTotalResults());
			
			List<CompactFood> foods = response.getResults();
			//This list contains summary information about the food items
			
			System.out.println("=========FOODS============");
			for (CompactFood food: foods) {
				System.out.println(food.getName());
			}
		}
		
		@Override
		public void onRecipeListRespone(Response<CompactRecipe> response) {
			System.out.println("TOTAL RECIPES: " + response.getTotalResults());
			
			List<CompactRecipe> recipes = response.getResults();
			System.out.println("=========RECIPES==========");
			for (CompactRecipe recipe: recipes) {
				System.out.println(recipe.getName());
			}
		}
		
		@Override
		public void onFoodResponse(Food food) {
			System.out.println("FOOD NAME: " + food.getName());
		}
		
		@Override
		public void onRecipeResponse(Recipe recipe) {
			System.out.println("RECIPE NAME: " + recipe.getName());
		}
	}
}