Reputation: 3352
I am currently using Retrofit to call to this URL. I am struggling understanding how to map your POJO based on the JSON. Here is my code:
final ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<Recipe> call = apiInterface.getRecipe();
call.enqueue(new Callback<Recipe>() {
@Override
public void onResponse(Call<Recipe> call, Response<Recipe> response) {
Log.v("SUCCESS", String.valueOf(response.isSuccessful()));
mRecipeListResponse = Collections.singletonList(response.body());
for (Recipe recipe: mRecipeListResponse){
Log.v("RECIPE", recipe.getId());
}
}
@Override
public void onFailure(Call<Recipe> call, Throwable t) {
Log.v("SUCCESS", String.valueOf(t.getMessage()));
}
});
}
public interface ApiInterface{
@GET("topher/2017/May/59121517_baking/baking.json")
Call<Recipe> getRecipe();
}
Data Structure / POJO:
public class Recipe {
protected List<Ingredients> ingredients;
private String id;
private String servings;
private String name;
private String image;
private List<Steps> steps;
public List<Ingredients> getIngredients() {
return ingredients;
}
public void setIngredients(List<Ingredients> ingredients) {
this.ingredients = ingredients;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getServings() {
return servings;
}
public void setServings(String servings) {
this.servings = servings;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public List<Steps> getSteps() {
return steps;
}
public void setSteps(List<Steps> steps) {
this.steps = steps;
}
@Override
public String toString() {
return "ClassPojo [ingredients = " + ingredients + ", id = " + id + ", servings = " + servings + ", name = " + name + ", image = " + image + ", steps = " + steps + "]";
}
}
Error:
07-21 15:06:00.238 20548-20548/kitchenpal.troychuinard.com.kitchenpal V/SUCCESS: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
EDIT: I have updated my POJO and am still receiving the same error
Upvotes: 0
Views: 72
Reputation: 3352
Turns out in my APiInterface
method I need to change toList<Recipe>
:
public interface ApiInterface{
@GET("topher/2017/May/59121517_baking/baking.json")
Call<List<Recipe>> getRecipe();
}
Upvotes: 0
Reputation: 306
You should change your model like this
public class Recipe {
private List<Ingredients> ingredients;
private String id;
private String servings;
private String name;
private String image;
private List<Steps> steps;
//Your getter and setters
}
Upvotes: 2