gson 我无法从Kitsu Api那里获得动画列表

ax6ht2ek  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(106)

我得到这个错误:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:路径$.data的第1行第10列应为开始_OBJECT,但实际上是BEGIN_ARRAY
https://kitsu.io/api/edge/anime
()创建一个新的服务示例;

public void makeApiCall() {
    Call<Animes> call = retroService.getAnimes();
    call.enqueue(new Callback<Animes>() {
        @Override
        public void onResponse(Call<Animes> call, Response<Animes> response) {
            Log.d("Result", response.body().getData().getAttributes().getCanonicalTitle());

        }

        @Override
        public void onFailure(Call<Animes> call, Throwable t) {

        }
    });
}

@GET(“动漫”)

```Call<Animes> getAnimes(); ```

我的POJO课程:

@Expose
    private Data data;

    /**
     * No args constructor for use in serialization
     *
     */
    public Animes() {
    }

    /**
     *
     * @param data
     */
    public Animes(Data data) {
        super();
        this.data = data;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

public class Data {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("attributes")
    @Expose
    private Attributes attributes;

    //With  getter and setters and constructor

public class Attributes {
    @SerializedName("synopsis")
    @Expose
    private String synopsis;
    @SerializedName("canonicalTitle")
    @Expose
    private String canonicalTitle;
    @SerializedName("startDate")
    @Expose
    private String startDate;
    @SerializedName("endDate")
    @Expose
    private String endDate;
    @SerializedName("ratingRank")
    @Expose
    private Integer ratingRank;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("episodeCount")
    @Expose
    private Integer episodeCount;
    @SerializedName("youtubeVideoId")
    @Expose
    private String youtubeVideoId;
//With  getter and setters and constructor
nqwrtyyt

nqwrtyyt1#

基本上,Expected BEGIN_OBJECT but was BEGIN_ARRAY意味着您试图解析单个对象(Animes),而JSON中没有单个对象,它是一个数组(根)
我不知道你的RetroInstanceRetroService是如何工作的,但是这个:

Call<Animes> call = ...

应该是

Call<ArrayList<Data>> call = ...

相关问题