user6761472 user6761472 - 3 months ago 11
Android Question

How to set jsonObject into retrofit DataModel in android

I want save

JsonObject
items into retrofit DataModel, but when running application show me error in
LogCat

my dataModels :

public class R_CatModel {

@SerializedName("id")
public Integer id;
@SerializedName("type")
public String type;
@SerializedName("slug")
public String slug;
@SerializedName("url")
public String url;
@SerializedName("status")
public String status;
@SerializedName("title")
public String title;
@SerializedName("title_plain")
public String title_plain;
@SerializedName("content")
public String content;
@SerializedName("excerpt")
public String excerpt;
@SerializedName("date")
public String date;
@SerializedName("modified")
public String modified;
@SerializedName("comment_count")
public int comment_count;
@SerializedName("comment_status")
public String comment_status;
@SerializedName("thumbnail")
public String thumbnail;
@SerializedName("thumbnail_images")
public R_CatThumbnailImages thumbnail_images;
@SerializedName("categories")
public R_CatCategory categories;

public R_CatModel(Integer id, String type, String slug, String url, String status, String title,
String title_plain, String content, String excerpt, String date, String modified,
int comment_count, String comment_status, String thumbnail,
R_CatThumbnailImages thumbnail_images, R_CatCategory categories) {
this.id = id;
this.type = type;
this.slug = slug;
this.url = url;
this.status = status;
this.title = title;
this.title_plain = title_plain;
this.content = content;
this.excerpt = excerpt;
this.date = date;
this.modified = modified;
this.comment_count = comment_count;
this.comment_status = comment_status;
this.thumbnail = thumbnail;
this.thumbnail_images = thumbnail_images;
this.categories = categories;
}

public Integer getId() {
return id;
}

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

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getSlug() {
return slug;
}

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

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getTitle() {
return title;
}

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

public String getTitle_plain() {
return title_plain;
}

public void setTitle_plain(String title_plain) {
this.title_plain = title_plain;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getExcerpt() {
return excerpt;
}

public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getModified() {
return modified;
}

public void setModified(String modified) {
this.modified = modified;
}

public int getComment_count() {
return comment_count;
}

public void setComment_count(int comment_count) {
this.comment_count = comment_count;
}

public String getComment_status() {
return comment_status;
}

public void setComment_status(String comment_status) {
this.comment_status = comment_status;
}

public String getThumbnail() {
return thumbnail;
}

public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}

public R_CatThumbnailImages getThumbnail_images() {
return thumbnail_images;
}

public void setThumbnail_images(R_CatThumbnailImages thumbnail_images) {
this.thumbnail_images = thumbnail_images;
}

public R_CatCategory getCategories() {
return categories;
}

public void setCategories(R_CatCategory category) {
this.categories = category;
}

}


CatCategory dataModel :

public class R_CatCategory {

public String getCatTitle() {
return catTitle;
}

public void setCatTitle(String catTitle) {
this.catTitle = catTitle;
}

@SerializedName("title")
public String catTitle;
}


My POJO class :

public class R_CatModelResponse {

@SerializedName("status")
public String Cat_status;
@SerializedName("count")
public int Cat_count;
@SerializedName("pages")
public int Cat_pages;
@SerializedName("category")
public Retrofit_ColoniesModel category;
@SerializedName("posts")
public List<R_CatModel> Cat_posts;

public String getCat_status() {
return Cat_status;
}

public void setCat_status(String cat_status) {
Cat_status = cat_status;
}

public int getCat_count() {
return Cat_count;
}

public void setCat_count(int cat_count) {
Cat_count = cat_count;
}

public int getCat_pages() {
return Cat_pages;
}

public void setCat_pages(int cat_pages) {
Cat_pages = cat_pages;
}

public Retrofit_ColoniesModel getCategory() {
return category;
}

public void setCategory(Retrofit_ColoniesModel category) {
this.category = category;
}

public List<R_CatModel> getCat_posts() {
return Cat_posts;
}

public void setCat_posts(List<R_CatModel> cat_posts) {
Cat_posts = cat_posts;
}
}


My Adapter codes:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

((DataViewHolder) holder).main_category.setText(Html.fromHtml(mDateSet.get(position).getCategories().getCatTitle()));


But when running application, show me this logCat :

Error : com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3901 path $.posts[0].categories


My Json :

{
"status": "ok",
"count": 10,
"pages": 3,
"category": {
"id": 1,
"slug": "%d8%b3%d8%b1%da%af%d8%b1%d9%85%db%8c",
"title": "\u0633\u0631\u06af\u0631\u0645\u06cc",
"description": "\u062a\u0648\u06cc \u0627\u06cc\u0646 \u06a9\u0644\u0648\u0646\u06cc \u0647\u0645\u0647 \u0686\u06cc\u0632 \u0648\u0627\u0633\u0647 \u0633\u0631\u06af\u0631\u0645 \u0628\u0648\u062f\u0646 \u0647\u0633\u062a. \u067e\u0633 \u0628\u062f\u0648 \u0628\u0631\u0648 \u062a\u0648\u0634",
"parent": 0,
"post_count": 29
},
"posts": [{
"id": 215,
"type": "post",
"slug": "%d8%a7%d9%86%d8%aa%d8%b4%d8%a7%d8%b1-%d8%a2%d9%be%d8%af%db%8c%d8%aa-%d8%a8%d8%b2%d8%b1%da%af-%d8%a8%d8%b1%d8%a7%db%8c-xbox-1",
"url": "http:\/\/tellfa.com\/tafrihgah\/?p=215",
"status": "publish",
"title": "\u0627\u0646\u062a\u0634\u0627\u0631 \u0622\u067e\u062f\u06cc\u062a \u0628\u0632\u0631\u06af \u0628\u0631\u0627\u06cc XBOX 1",
"title_plain": "\u0627\u0646\u062a\u0634\u0627\u0631 \u0622\u067e\u062f\u06cc\u062a \u0628\u0632\u0631\u06af \u0628\u0631\u0627\u06cc XBOX 1",
"content": "<p>\u0645\u0627\u06cc\u06a9\u0631\u0648\u0633\u0627\u0641\u062a \u0628\u0631\u0648\u0632\u0631\u0633\u0627\u0646 \u0628\u0632\u0631\u06af \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646 \u0631\u0627 \u0627\u0632 \u0627\u0645\u0631\u0648\u0632 \u0645\u0646\u062a\u0634\u0631 \u06a9\u0631\u062f\u0647 \u0627\u0633\u062a. \u0627\u06cc\u0646 \u0628\u0631\u0648\u0632\u0631\u0633\u0627\u0646\u060c \u0642\u0627\u0628\u0644\u06cc\u062a\u200c\u0647\u0627\u06cc \u0632\u06cc\u0627\u062f\u06cc \u0631\u0627 \u0628\u0647 \u06a9\u0646\u0633\u0648\u0644 \u0646\u0633\u0644 \u0647\u0634\u062a\u0645\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0627\u0636\u0627\u0641\u0647 \u062e\u0648\u0627\u0647\u062f \u06a9\u0631\u062f\u061b \u0627\u06cc\u0646 \u0642\u0627\u0628\u0644\u06cc\u062a\u200c\u0647\u0627\u060c \u0645\u0646\u0634\u06cc \u0647\u0648\u0634\u0645\u0646\u062f \u0645\u0627\u06cc\u06a9\u0631\u0648\u0633\u0627\u0641\u062a \u06cc\u0639\u0646\u06cc Cortana \u0631\u0627 \u0647\u0645 \u0634\u0627\u0645\u0644 \u0645\u06cc\u200c\u0634\u0648\u0646\u062f.<\/p>\n<p>\u0646\u0633\u062e\u0647\u200c\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u06a9\u0648\u0631\u062a\u0627\u0646\u0627\u060c \u06a9\u0627\u0645\u0644\u0627 \u0634\u0628\u06cc\u0647 \u0628\u0647 \u0646\u0633\u062e\u0647\u200c\u06cc \u0648\u06cc\u0646\u062f\u0648\u0632\u06cc \u0622\u0646 \u0637\u0631\u0627\u062d\u06cc \u0634\u062f\u0647 \u0627\u0633\u062a. \u0645\u06cc\u200c\u062a\u0648\u0627\u0646 \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 \u062d\u0633\u06af\u0631 \u06a9\u06cc\u0646\u06a9\u062a \u06cc\u0627 \u06cc\u06a9 \u0647\u062f\u0633\u062a\u060c \u0628\u0647 \u06a9\u0648\u0631\u062a\u0627\u0646\u0627 \u0641\u0631\u0645\u0627\u0646 \u0635\u0648\u062a\u06cc \u062f\u0627\u062f \u0648 \u0633\u0648\u0627\u0644\u200c\u0647\u0627\u06cc \u0645\u062e\u062a\u0644\u0641\u06cc \u0627\u0632 \u0627\u06cc\u0646 \u0645\u0646\u0634\u06cc \u0647\u0648\u0634\u0645\u0646\u062f \u067e\u0631\u0633\u06cc\u062f. \u0641\u0631\u0645\u0627\u0646\u200c\u0647\u0627\u06cc \u0635\u0648\u062a\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646 \u0647\u0645 \u0627\u0632 \u0627\u06cc\u0646 \u067e\u0633 \u062c\u0627\u06cc \u062e\u0648\u062f\u0634\u0627\u0646 \u0631\u0627 \u0628\u0647 \u06a9\u0648\u0631\u062a\u0627\u0646 \u0645\u06cc\u200c\u062f\u0647\u0646\u062f. \u0627\u0644\u0628\u062a\u0647 \u0628\u0627 \u063a\u06cc\u0631\u0641\u0639\u0627\u0644 \u06a9\u0631\u062f\u0646 \u06a9\u0648\u0631\u062a\u0627\u0646\u0627\u060c \u0645\u06cc\u200c\u062a\u0648\u0627\u0646 \u062f\u0648\u0628\u0627\u0631\u0647 \u0627\u0632 \u0641\u0631\u0645\u0627\u0646\u200c\u0647\u0627\u06cc \u0635\u0648\u062a\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u06a9\u0631\u062f \u0648 \u0628\u0647 \u062c\u0627\u06cc \u06a9\u0644\u0645\u0647\u200c\u06cc Hey Cortana \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 \u0639\u0628\u0627\u0631\u062a Xbox \u0628\u0647 \u06a9\u0646\u0633\u0648\u0644 \u0641\u0631\u0645\u0627\u0646 \u062f\u0627\u062f.<br \/>\n\u06cc\u06a9\u06cc \u062f\u06cc\u06af\u0631 \u0627\u0632 \u0642\u0627\u0628\u0644\u06cc\u062a\u200c\u0647\u0627\u06cc \u0628\u0632\u0631\u06af\u06cc \u06a9\u0647 \u0628\u0647 \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646 \u0627\u0636\u0627\u0641\u0647 \u0645\u06cc\u200c\u0634\u0648\u062f\u060c \u0627\u0645\u06a9\u0627\u0646 \u067e\u062e\u0634 \u0645\u0648\u0633\u06cc\u0642\u06cc\u200c\u0647\u0627\u06cc \u062f\u0644\u062e\u0648\u0627\u0647 \u062f\u0631 \u067e\u0633\u200c\u0632\u0645\u06cc\u0646\u0647\u200c\u06cc \u0628\u0627\u0632\u06cc\u200c\u0647\u0627 \u0627\u0633\u062a. \u0645\u06cc\u200c\u062a\u0648\u0627\u0646 \u0647\u0631 \u0622\u0647\u0646\u06af\u06cc \u0631\u0627 \u062f\u0631 \u062d\u06cc\u0646 \u0628\u0627\u0632\u06cc \u06a9\u0631\u062f\u0646 \u067e\u062e\u0634 \u06a9\u0631\u062f. \u0627\u0645\u06a9\u0627\u0646 \u06a9\u0646\u062a\u0631\u0644 \u0645\u0648\u0633\u06cc\u0642\u06cc \u0647\u0645 \u0627\u0632 \u0645\u0646\u0648\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u062c\u0648\u062f \u062f\u0627\u0631\u062f.<br \/>\n\u0628\u0627 \u0628\u0631\u0648\u0632\u0631\u0633\u0627\u0646 \u062c\u062f\u06cc\u062f \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646\u060c \u0641\u0631\u0648\u0634\u06af\u0627\u0647 \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648 \u0648\u06cc\u0646\u062f\u0648\u0632 \u0628\u0627 \u06cc\u06a9\u062f\u06cc\u06af\u0631 \u0627\u062f\u063a\u0627\u0645 \u0645\u06cc\u200c\u0634\u0648\u0646\u062f\u061b \u0637\u0648\u0631\u06cc \u06a9\u0647 \u0645\u06cc\u200c\u062a\u0648\u0627\u0646 \u0628\u0647 \u0628\u0631\u062e\u06cc \u0627\u0632 \u0646\u0631\u0645\u200c\u0627\u0641\u0632\u0627\u0631\u0647\u0627\u06cc\u06cc \u06a9\u0647 \u0628\u0631\u0627\u06cc \u0648\u06cc\u0646\u062f\u0648\u0632 \u06f1\u06f0 \u0637\u0631\u0627\u062d\u06cc \u0634\u062f\u0647\u200c\u0627\u0646\u062f \u0647\u0645 \u0631\u0648\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646 \u062f\u0633\u062a\u0631\u0633\u06cc \u062f\u0627\u0634\u062a. \u0633\u0631\u0648\u06cc\u0633 Xbox Play Anywhere \u0647\u0645 \u0628\u0627 \u0628\u0631\u0648\u0632\u0631\u0633\u0627\u0646 \u062a\u0627\u0628\u0633\u062a\u0627\u0646 \u0628\u0647 \u06a9\u0646\u0633\u0648\u0644 \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646 \u0627\u0636\u0627\u0641\u0647 \u0645\u06cc\u200c\u0634\u0648\u062f. \u0627\u06cc\u0646 \u0633\u0631\u0648\u06cc\u0633 \u0628\u0647 \u0627\u0641\u0631\u0627\u062f \u0627\u062c\u0627\u0632\u0647 \u0645\u06cc\u200c\u062f\u0647\u062f \u062a\u0627 \u0628\u0627\u0632\u06cc\u200c\u0647\u0627\u06cc\u06cc \u06a9\u0647 \u0627\u0632 Xbox Play Anywhere \u067e\u0634\u062a\u06cc\u0628\u0627\u0646\u06cc \u0645\u06cc\u200c\u06a9\u0646\u0646\u062f \u0631\u0627 \u06cc\u06a9 \u0628\u0627\u0631 \u0628\u062e\u0631\u0646\u062f \u0648 \u0647\u0645 \u0631\u0648\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648 \u0647\u0645 \u0631\u0648\u06cc \u0648\u06cc\u0646\u062f\u0648\u0632 \u06f1\u06f0 \u0627\u0632 \u0622\u0646 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u06a9\u0646\u0646\u062f.<\/p>\n<p>\u0627\u06af\u0631 \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646 \u062f\u0627\u0631\u06cc\u062f\u060c \u0645\u06cc\u200c\u062a\u0648\u0627\u0646\u06cc\u062f \u0647\u0645\u06cc\u0646 \u062d\u0627\u0644\u0627 \u0628\u0631\u0648\u0632\u0631\u0633\u0627\u0646 \u0622\u0646 \u0631\u0627 \u062f\u0627\u0646\u0644\u0648\u062f \u06a9\u0646\u06cc\u062f. <\/p>\n",
"excerpt": "<p>\u0645\u0627\u06cc\u06a9\u0631\u0648\u0633\u0627\u0641\u062a \u0628\u0631\u0648\u0632\u0631\u0633\u0627\u0646 \u0628\u0632\u0631\u06af \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0648\u0627\u0646 \u0631\u0627 \u0627\u0632 \u0627\u0645\u0631\u0648\u0632 \u0645\u0646\u062a\u0634\u0631 \u06a9\u0631\u062f\u0647 \u0627\u0633\u062a. \u0627\u06cc\u0646 \u0628\u0631\u0648\u0632\u0631\u0633\u0627\u0646\u060c \u0642\u0627\u0628\u0644\u06cc\u062a\u200c\u0647\u0627\u06cc \u0632\u06cc\u0627\u062f\u06cc \u0631\u0627 \u0628\u0647 \u06a9\u0646\u0633\u0648\u0644 \u0646\u0633\u0644 \u0647\u0634\u062a\u0645\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u0627\u0636\u0627\u0641\u0647 \u062e\u0648\u0627\u0647\u062f \u06a9\u0631\u062f\u061b \u0627\u06cc\u0646 \u0642\u0627\u0628\u0644\u06cc\u062a\u200c\u0647\u0627\u060c \u0645\u0646\u0634\u06cc \u0647\u0648\u0634\u0645\u0646\u062f \u0645\u0627\u06cc\u06a9\u0631\u0648\u0633\u0627\u0641\u062a \u06cc\u0639\u0646\u06cc Cortana \u0631\u0627 \u0647\u0645 \u0634\u0627\u0645\u0644 \u0645\u06cc\u200c\u0634\u0648\u0646\u062f.<br \/>\n\u0646\u0633\u062e\u0647\u200c\u06cc \u0627\u06cc\u06a9\u0633\u200c\u0628\u0627\u06a9\u0633 \u06a9\u0648\u0631\u062a\u0627\u0646\u0627\u060c \u06a9\u0627\u0645\u0644\u0627 \u0634\u0628\u06cc\u0647 \u0628\u0647 \u0646\u0633\u062e\u0647\u200c\u06cc \u0648\u06cc\u0646\u062f\u0648\u0632\u06cc \u0622\u0646 \u0637\u0631\u0627\u062d\u06cc <\/p>\n",
"date": "2016-08-01 19:55:16",
"modified": "2016-08-01 19:55:16",
"categories": [{
"id": 1,
"slug": "%d8%b3%d8%b1%da%af%d8%b1%d9%85%db%8c",
"title": "\u0633\u0631\u06af\u0631\u0645\u06cc",
"description": "\u062a\u0648\u06cc \u0627\u06cc\u0646 \u06a9\u0644\u0648\u0646\u06cc \u0647\u0645\u0647 \u0686\u06cc\u0632 \u0648\u0627\u0633\u0647 \u0633\u0631\u06af\u0631\u0645 \u0628\u0648\u062f\u0646 \u0647\u0633\u062a. \u067e\u0633 \u0628\u062f\u0648 \u0628\u0631\u0648 \u062a\u0648\u0634",
"parent": 0,
"post_count": 29
}],


How can I fix this issue and load jsonObject in my Adapter?

Thanks all <3

Answer

You're expecting a single category for categories.

@SerializedName("categories")
public R_CatCategory categories;

While you get a list instead.

"categories": [{
    "id": 1,
    "slug": "%d8%b3%d8%b1%da%af%d8%b1%d9%85%db%8c",
    "title": "\u0633\u0631\u06af\u0631\u0645\u06cc",
    "description": "\u062a\u0648\u06cc \u0627\u06cc\u0646 \u06a9\u0644\u0648\u0646\u06cc \u0647\u0645\u0647 \u0686\u06cc\u0632 \u0648\u0627\u0633\u0647 \u0633\u0631\u06af\u0631\u0645 \u0628\u0648\u062f\u0646 \u0647\u0633\u062a. \u067e\u0633 \u0628\u062f\u0648 \u0628\u0631\u0648 \u062a\u0648\u0634",
    "parent": 0,
    "post_count": 29
}],

So instead define a type able to handle a JSON array.

@SerializedName("categories")
public List<R_CatCategory> categories;
Comments