Ejemplo n.º 1
0
  private Recipe getRecipe(JSONArray recipesArray, int recipe_id) {

    Recipe recipe = new Recipe();
    for (int i = 0; i < recipesArray.length(); i++) {

      try {
        if (recipe_id == (recipesArray.getJSONObject(i).getJSONObject("recipe")).getInt(KEY_ID)) {
          JSONObject jsonObject = recipesArray.getJSONObject(i).getJSONObject("recipe");
          recipe.setId(jsonObject.getInt(KEY_ID));
          recipe.setTitle(jsonObject.getString(KEY_TITLE));
          recipe.setDescription((jsonObject.getString(KEY_DESCRIPTION)));
          recipe.setImageUrl(jsonObject.getString("thumbnail_image_url"));
          recipe.setInstructions(jsonObject.getJSONArray(KEY_INSTRUCTIONS));
          recipe.setCalories(jsonObject.getInt(KEY_CALORIES));
          recipe.setProtein(jsonObject.getInt(KEY_PROTEIN));
          recipe.setCarbs(jsonObject.getInt(KEY_CARB));
          recipe.setFat(jsonObject.getInt(KEY_FAT));
          recipe.setIngredients(jsonObject.getJSONArray(KEY_INGREDIENTS));
        }
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return recipe;
  }