Recipe - Food - Nutrition

FREEMIUM
Verified
By David | Updated 8 days ago | Food
Popularity

9.9 / 10

Latency

488ms

Service Level

100%

Health Check

N/A

Back to All Discussions

Get Recipes Returned JSON

Rapid account: Deleted
[deleted]
9 years ago

Hi I am having trouble using the JsonArray that is returned from calling “https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients=eggs, milk”.

The error I keep getting is “JsonArray cannot be cast as JsonObject”. I am successful in retrieving any queries that involve just one item but when I need a list of items, I run into this problem.

Any ideas on how to fix it? I’ve checked StackOverflow and have tried all solutions on the first page of the google results.

Rapid account: Spoonacular
spoonacular Commented 8 years ago

Hi SmartChef,

do you still need evaluation access to the API?

Best,
David

Rapid account: Spoonacular
spoonacular Commented 9 years ago

great to hear 😃

Rapid account: Deleted
[deleted] Commented 9 years ago

I had one last attempt at it before I saw your response and it worked. If I have problems in the future, I will try what you just suggested.

What worked was:

@GET("/recipes/{id}")
public JsonArray getRecipeByIngredient(@Path(value = “id”, encode = false) String id);

and the string that was passed in was “findByIngredients?ingredients=whatever_was_searched_separated_by_%2C”

Rapid account: Spoonacular
spoonacular Commented 9 years ago

Can’t you do something like this:

@Get("/{search}/") public JsonArray getRecipeByIngredient(@Path(“search”) String search, @Query(“ingredients”) String ingredients, @Query(“number”) int number);

and then pass

search: /recipes/findByIngredients

ingredients: eggs

number: 5

Rapid account: Deleted
[deleted] Commented 9 years ago

So what would be a solution? Not sure what I can do

Rapid account: Spoonacular
spoonacular Commented 9 years ago

maybe that code url encodes your parameter and you get the “?” replace with its url encoded version? That would then lead to an invalid request…

Rapid account: Deleted
[deleted] Commented 9 years ago

I am using

@GET("/recipes/findByIngredients")
public JsonArray getRecipeByIngredient(@Query(“search”) String search);

When I do this it works:
@Get("/recipes/findByIngredients?ingredients=eggs%2Cmilk&number=5)
public JsonArray getRecipeByIngredient();

Another method that I wrote that works is

@GET("/recipes/{id}/information")
public RecipeModel getRecipe(@Path(“id”) String id);

So I tried:

@Get("/{search}")
public JsonArray(getRecipeByIngredient(@Path(“search”) String search);

and passed “recipes/findByIngredients?ingredients=eggs%2Cmilk&number=5” into the method
but got a 400 bad request error. Same exact request but now there is an error.

Rapid account: Spoonacular
spoonacular Commented 9 years ago

Can you send me the URL for which you get the 500er error? the one from your first post works for me.

Rapid account: Deleted
[deleted] Commented 9 years ago

So I am now getting a “500 Internal Server” error when I query the server. My original problem was that I somehow forgot to provide search parameters when I made the query.

Any ideas on why the 500 Internal Server error is being thrown?

Rapid account: Deleted
[deleted] Commented 9 years ago

I could not use Unirest because Android was not allowing it. I am using Retrofit to make the calls to server. I have tried returning a JsonArray and still got errors. Do you know how to use Retrofit?

Join in the discussion - add comment below:

Login / Signup to post new comments