Exemplo n.º 1
0
  public List<Recipe> getSearchList(int page) {
    List<Recipe> list = new ArrayList<>();
    Recipe recipe;
    URL url = null;
    try {
      url = new URL(builder.getSearchURL(page, ingredients, sort));
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    String recipeJsonData = getRecipeJsonData(url);

    // These are the names of the JSON objects that need to be extracted.
    final String OWM_LIST = "recipes";
    final String OWM_PUBLISHER = "publisher";
    final String OWM_TITLE = "title";
    final String OWM_SOURCE_URL = "source_url";
    final String OWM_RECIPE_ID = "recipe_id";
    final String OWM_IMAGE_URL = "image_url";
    final String OWM_SOCIAL_RANK = "social_rank";
    final String OWM_PUBLISHER_URL = "publisher_url";

    JSONObject jsonObject = null;

    try {
      jsonObject = new JSONObject(recipeJsonData);

      JSONArray recipesArray = jsonObject.getJSONArray(OWM_LIST);

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

        JSONObject recipeObj = (JSONObject) recipesArray.get(i);
        recipe.setPublisher(recipeObj.getString(OWM_PUBLISHER));
        recipe.setTitle(recipeObj.getString(OWM_TITLE));
        recipe.setSource_url(recipeObj.getString(OWM_SOURCE_URL));
        recipe.setRecipe_id(recipeObj.getString(OWM_RECIPE_ID));
        recipe.setImage_url(recipeObj.getString(OWM_IMAGE_URL));
        recipe.setSocial_rank(recipeObj.getString(OWM_SOCIAL_RANK));
        recipe.setPublisher_url(recipeObj.getString(OWM_PUBLISHER_URL));
        list.add(recipe);
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return list;
  }
Exemplo n.º 2
0
  public Recipe getRecipe(String rId) {
    Recipe recipe;
    String recipeJsonData;
    URL url = null;
    try {
      url = new URL(builder.getGET_URL(rId));
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    recipeJsonData = getRecipeJsonData(url);
    final String OWM_LIST = "recipe";
    final String OWM_PUBLISHER = "publisher";
    final String OWM_TITLE = "title";
    final String OWM_SOURCE_URL = "source_url";
    final String OWM_RECIPE_ID = "recipe_id";
    final String OWM_IMAGE_URL = "image_url";
    final String OWM_SOCIAL_RANK = "social_rank";
    final String OWM_PUBLISHER_URL = "publisher_url";
    final String OWM_INGREDIENTS = "ingredients";

    JSONObject jsonObject = null;
    recipe = new Recipe();
    try {
      jsonObject = new JSONObject(recipeJsonData);
      JSONObject recipeObj = (JSONObject) jsonObject.getJSONObject(OWM_LIST);

      recipe.setPublisher(recipeObj.getString(OWM_PUBLISHER));
      recipe.setTitle(recipeObj.getString(OWM_TITLE));
      recipe.setSource_url(recipeObj.getString(OWM_SOURCE_URL));
      recipe.setRecipe_id(recipeObj.getString(OWM_RECIPE_ID));
      recipe.setImage_url(recipeObj.getString(OWM_IMAGE_URL));
      recipe.setSocial_rank(recipeObj.getString(OWM_SOCIAL_RANK));
      recipe.setPublisher_url(recipeObj.getString(OWM_PUBLISHER_URL));
      recipe.setIngredients(recipeObj.getString(OWM_INGREDIENTS));

    } catch (JSONException e) {
      e.printStackTrace();
    }
    return recipe;
  }