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.
ディスカッションに参加しましょう-以下にコメントを追加してください:
Hi SmartChef,
do you still need evaluation access to the API?
Best,
David
great to hear 😃
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”
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
So what would be a solution? Not sure what I can do
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…
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.
Can you send me the URL for which you get the 500er error? the one from your first post works for me.
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?
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?