Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface RecipeService {
- @GET("recipes")
- Call<List<Recipe>> getRecipes();
- @GET("recipes/{recipeId}")
- Call<Recipe> getRecipeById(@Path("recipeId") long recipeId);
- @POST("recipes")
- Call<Recipe> createRecipe(@Body Recipe recipe);
- @DELETE("recipes/delete/{recipeId}")
- Call<Recipe> deleteRecipe(@Path("recipeId") long recipeId);
- @PUT("recipes/{recipeId}/editname")
- Call<Recipe> changeRecipeName(@Path("recipeId") long recipeId, String recipeName);
- @GET("recipes/{recipeId}/ingredients")
- Call<Ingredient> getAllIngredientsInRecipe(@Path("recipeId") long recipeId);
- @POST("recipes/{recipeId}/ingredients/add")
- Call<Ingredient> addIngredientToRecipe(@Path("recipeId") long recipeId, String ingredientName);
- @POST("recipes/{recipeId}/ingredients/addmany")
- Call<Ingredient> addIngredientsToRecipe(@Path("recipeId") long recipeId, List<String> ingredients);
- @DELETE("recipes/{recipeId}/ingredients/delete/{ingredientName}")
- Call<Ingredient> removeIngredientFromRecipe(@Path("recipeId") long recipeId, @Path("ingredientName") String ingredientName);
- @DELETE("recipes/{recipeId}/ingredients/deletemany")
- Call<Ingredient> removeIngredientsFromRecipe(@Path("recipeId") long recipeId, List<String> ingredients);
- }
- GET metod för att hämta hem en lista från ett Rest API
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(Constants.RESTAPI_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- RecipeService service = retrofit.create(RecipeService.class);
- Call<List<Recipe>> call = service.getRecipes();
- call.enqueue(new Callback<List<Recipe>>() {
- @Override
- public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) {
- adapter = new RecipesRecyclerAdapter(response.body());
- recyclerView.setAdapter(adapter);
- }
- @Override
- public void onFailure(Call<List<Recipe>> call, Throwable t) {
- }
- });
- POST metod för att posta till ett rest API, kan även göras genom en service för att få det mer async
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(Constants.RESTAPI_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- RecipeService service = retrofit.create(RecipeService.class);
- Recipe recipe = new Recipe("Testing Post Function");
- Call<Recipe> call = service.createRecipe(recipe);
- call.enqueue(new Callback<Recipe>() {
- @Override
- public void onResponse(Call<Recipe> call, Response<Recipe> response) {
- if(call.isExecuted() && response.isSuccessful()){
- Log.d("Rest POST", response.message());
- }
- }
- @Override
- public void onFailure(Call<Recipe> call, Throwable t) {
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement