Advertisement
Neshoz

Untitled

Jan 16th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. public interface RecipeService {
  2.     @GET("recipes")
  3.     Call<List<Recipe>> getRecipes();
  4.  
  5.     @GET("recipes/{recipeId}")
  6.     Call<Recipe> getRecipeById(@Path("recipeId") long recipeId);
  7.  
  8.     @POST("recipes")
  9.     Call<Recipe> createRecipe(@Body Recipe recipe);
  10.  
  11.     @DELETE("recipes/delete/{recipeId}")
  12.     Call<Recipe> deleteRecipe(@Path("recipeId") long recipeId);
  13.  
  14.     @PUT("recipes/{recipeId}/editname")
  15.     Call<Recipe> changeRecipeName(@Path("recipeId") long recipeId, String recipeName);
  16.  
  17.     @GET("recipes/{recipeId}/ingredients")
  18.     Call<Ingredient> getAllIngredientsInRecipe(@Path("recipeId") long recipeId);
  19.  
  20.     @POST("recipes/{recipeId}/ingredients/add")
  21.     Call<Ingredient> addIngredientToRecipe(@Path("recipeId") long recipeId, String ingredientName);
  22.  
  23.     @POST("recipes/{recipeId}/ingredients/addmany")
  24.     Call<Ingredient> addIngredientsToRecipe(@Path("recipeId") long recipeId, List<String> ingredients);
  25.  
  26.     @DELETE("recipes/{recipeId}/ingredients/delete/{ingredientName}")
  27.     Call<Ingredient> removeIngredientFromRecipe(@Path("recipeId") long recipeId, @Path("ingredientName") String ingredientName);
  28.  
  29.     @DELETE("recipes/{recipeId}/ingredients/deletemany")
  30.     Call<Ingredient> removeIngredientsFromRecipe(@Path("recipeId") long recipeId, List<String> ingredients);
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. GET metod för att hämta hem en lista från ett Rest API
  41. Retrofit retrofit = new Retrofit.Builder()
  42.                 .baseUrl(Constants.RESTAPI_BASE_URL)
  43.                 .addConverterFactory(GsonConverterFactory.create())
  44.                 .build();
  45.         RecipeService service = retrofit.create(RecipeService.class);
  46.         Call<List<Recipe>> call = service.getRecipes();
  47.         call.enqueue(new Callback<List<Recipe>>() {
  48.             @Override
  49.             public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) {
  50.                 adapter = new RecipesRecyclerAdapter(response.body());
  51.                 recyclerView.setAdapter(adapter);
  52.             }
  53.  
  54.             @Override
  55.             public void onFailure(Call<List<Recipe>> call, Throwable t) {
  56.  
  57.             }
  58.         });
  59.  
  60.  
  61.  
  62. POST metod för att posta till ett rest API, kan även göras genom en service för att få det mer async
  63. Retrofit retrofit = new Retrofit.Builder()
  64.                 .baseUrl(Constants.RESTAPI_BASE_URL)
  65.                 .addConverterFactory(GsonConverterFactory.create())
  66.                 .build();
  67.         RecipeService service = retrofit.create(RecipeService.class);
  68.         Recipe recipe = new Recipe("Testing Post Function");
  69.         Call<Recipe> call = service.createRecipe(recipe);
  70.         call.enqueue(new Callback<Recipe>() {
  71.             @Override
  72.             public void onResponse(Call<Recipe> call, Response<Recipe> response) {
  73.                 if(call.isExecuted() && response.isSuccessful()){
  74.                     Log.d("Rest POST", response.message());
  75.                 }
  76.             }
  77.  
  78.             @Override
  79.             public void onFailure(Call<Recipe> call, Throwable t) {
  80.  
  81.             }
  82.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement