gpt4 book ai didi

java - GSON 非法状态异常 : Expected BEGIN_OBJECT but was BEGIN_ARRAY

转载 作者:行者123 更新时间:2023-12-02 02:01:51 27 4
gpt4 key购买 nike

我目前正在使用 Retrofit 来调用此 URL 。我正在努力理解如何基于 JSON 映射 POJO。这是我的代码:

  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();
}

数据结构/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 + "]";
}

}

错误:

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 $

编辑:我已经更新了我的 POJO,但仍然收到相同的错误

最佳答案

你应该像这样改变你的模型

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

}

关于java - GSON 非法状态异常 : Expected BEGIN_OBJECT but was BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51459721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com