Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

android - 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

This is my JSON response:

{
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
}
]
}

And my retrofit 2 interface:

interface VoDService {

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

}

function to get the category lists and have also checked if the response is coming by using "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());
    }
});

And finally my Model 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;
    }
}

Now I can't get the response in the response.body. It's about the Model class. Should I write a custom parser? or any solution which gets me the Json in a list, I just need the count, category title. Thanks in advance and please don't mark it as duplicate or anything.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

your model is wrong it should look like this pseudo code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...