Retrofit 2 响应 body 为空或模型 class 错误。无法获取 JSON 数据,它给出了异常:预期 BEGIN_ARRAY 但 BEGIN_OBJECT

Retrofit 2 response body is empty or Model class is wrong. Cannot get JSON data it gives an Exception: Expected BEGIN_ARRAY but was BEGIN_OBJECT

这是我的 JSON 回复:

{
status: "ok",
count: 25,
categories: [
{
id: 60,
slug: "3d",
title: "3D",
description: "",
parent: 70,
post_count: 86
},
{
id: 2,
slug: "action-adventure",
title: "Action/Adventure",
description: "",
parent: 70,
post_count: 980
},
{
id: 61,
slug: "bollywood",
title: "Bollywood",
description: "",
parent: 0,
post_count: 747
},
{
id: 3,
slug: "comedy",
title: "Comedy",
description: "",
parent: 70,
post_count: 790
},
{
id: 14,
slug: "documentaries",
title: "Documentary",
description: "",
parent: 0,
post_count: 51
},
{
id: 4,
slug: "drama",
title: "Drama",
description: "",
parent: 70,
post_count: 1157
},
{
id: 70,
slug: "english-movies",
title: "English Movies",
description: "",
parent: 0,
post_count: 968
},
{
id: 5,
slug: "featured",
title: "Featured",
description: "",
parent: 0,
post_count: 166
},
{
id: 71,
slug: "hidden",
title: "Hidden",
description: "",
parent: 0,
post_count: 2
},
{
id: 12,
slug: "horror-thriller",
title: "Horror, Thriller",
description: "",
parent: 70,
post_count: 614
},
{
id: 63,
slug: "islam",
title: "Islam",
description: "",
parent: 0,
post_count: 159
},
{
id: 66,
slug: "islamic-documentaries",
title: "Islamic Documentaries",
description: "",
parent: 63,
post_count: 5
},
{
id: 67,
slug: "islamic-lectures",
title: "Islamic Lectures",
description: "",
parent: 63,
post_count: 1
},
{
id: 64,
slug: "islamic-movies",
title: "Islamic Movies",
description: "",
parent: 63,
post_count: 2
},
{
id: 6,
slug: "kids-family",
title: "Kids/Family",
description: "",
parent: 70,
post_count: 458
},
{
id: 7,
slug: "on-demand",
title: "On Demand",
description: "All requested movies by iVOD users falls here.",
parent: 0,
post_count: 1404
},
{
id: 8,
slug: "oscars-winners-collection",
title: "Oscars' Winners",
description: "",
parent: 70,
post_count: 87
},
{
id: 65,
slug: "quran",
title: "Quran",
description: "",
parent: 63,
post_count: 100
},
{
id: 9,
slug: "romance",
title: "Romance",
description: "",
parent: 70,
post_count: 316
},
{
id: 10,
slug: "science-fictionfantasy",
title: "Sci-Fi/Fantasy",
description: "",
parent: 70,
post_count: 427
},
{
id: 11,
slug: "short-movies",
title: "Short Films",
description: "",
parent: 70,
post_count: 22
},
{
id: 69,
slug: "tv",
title: "TV",
description: "",
parent: 0,
post_count: 529
},
{
id: 13,
slug: "tv-series",
title: "TV Series",
description: "",
parent: 69,
post_count: 499
},
{
id: 68,
slug: "visualquran-tv",
title: "VisualQuran.tv",
description: "",
parent: 63,
post_count: 53
},
{
id: 15,
slug: "western",
title: "Western",
description: "",
parent: 70,
post_count: 81
}
]
}

还有我的改装2界面:

interface VoDService {

    @GET("get_category_index/")
    Call<Category> vodCategories();

}

获取类别列表的函数,并使用 "HttpLoggingInterceptor":

检查是否有响应
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

httpClient.addInterceptor(logging);

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://vod.nayatel.com/api/")
        .client(httpClient.build())
        .addConverterFactory(GsonConverterFactory.create())
        .build();

VoDService client = retrofit.create(VoDService.class);

Call<Category> call = client.vodCategories();
call.enqueue(new Callback<Category>() {
    @Override
    public void onResponse(Call<Category> call, Response<Category> response) {
        if (response.isSuccessful()) {
            Log.d(TAG, "onResponse: success!"+ response.raw().body().toString());
        } else {
            Log.d(TAG, "onResponse: unsuccessful");
        }
    }

    @Override
    public void onFailure(Call<Category> call, Throwable t) {
        Log.d(TAG, "onFailure: " + t.toString());
    }
});

最后是我的模特 class:

import com.google.gson.annotations.SerializedName;


class Category {

    @SerializedName("id")
    private Integer id;
    @SerializedName("slug")
    private String slug;
    @SerializedName("title")
    private String title;
    @SerializedName("description")
    private String description;
    @SerializedName("parent")
    private Integer parent;
    @SerializedName("post_count")
    private Integer postCount;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getSlug() {
        return slug;
    }

    public void setSlug(String slug) {
        this.slug = slug;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getParent() {
        return parent;
    }

    public void setParent(Integer parent) {
        this.parent = parent;
    }

    public Integer getPostCount() {
        return postCount;
    }

    public void setPostCount(Integer postCount) {
        this.postCount = postCount;
    }
}

现在我无法在 response.body 中得到响应。这是关于模型 class 的。我应该写一个自定义解析器吗?或任何让我在列表中获得 Json 的解决方案,我只需要计数、类别标题。 在此先感谢,请不要将其标记为重复或任何内容。

你的模型有误 它应该看起来像这个伪代码

public Response{
    String status;
    int count;
    List<Category> categories;