Android volley: Expected BEGIN_ARRAY but was BEGIN_OBJECT

503 Views Asked by At

I am developing Android application where I need to get posts from Wordpress blog (with specific tag). JSON API plugin is installed: https://wordpress.org/plugins/json-api/

In my application I use Volley library. I am getting this error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT

My code:

 String url = " http://christianconcepts.com/api/get_tag_posts/?tag_slug=appcontent ";
        ListView postList;
        List<Object> list;
        Gson gson;
        Map<String,Object> mapPost;
        Map<String,Object> mapTitle;
        String postTitle[];

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

                @Override
                public void onResponse(String s) {
                    gson = new Gson();
                    list = (List) gson.fromJson(s, List.class); // error line
                    postTitle = new String[list.size()];

                    for(int i=0;i<list.size();++i){

                        mapPost = (Map<String,Object>)list.get(i);
                        mapTitle = (Map<String, Object>) mapPost.get("title");
                        postTitle[i] = (String) mapTitle.get("rendered");
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(getActivity().getApplicationContext(), "Some error occurred", Toast.LENGTH_LONG).show();
                }
            });

            RequestQueue rQueue = Volley.newRequestQueue(myView.getContext());
            rQueue.add(request);

Could someone help me and tell what do I need to change in order to make it work?

3

There are 3 best solutions below

0
On

try this:

list = Arrays.asList(gson.fromJson(s,
                    List[].class));

instead of..

list = (List) gson.fromJson(s, List.class); // error line
0
On

Use this model classes Model Classes Created using JsonSchema2POJO.

Example example = new GsonBuilder().fromJson(s,Example.class);

Complete Json will parse into Java Object and use java object to get the data.

Author.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;



package com.example;

public class Author {

@SerializedName("id")
@Expose
private int id;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("name")
@Expose
private String name;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("nickname")
@Expose
private String nickname;
@SerializedName("url")
@Expose
private String url;
@SerializedName("description")
@Expose
private String description;

public int getId() {
    return id;
}

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

public String getSlug() {
    return slug;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getNickname() {
    return nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public String getUrl() {
    return url;
}

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

public String getDescription() {
    return description;
}

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

}

CustomFields.java

    package com.example;


public class CustomFields {


}


    package com.example;

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("status")
@Expose
private String status;
@SerializedName("count")
@Expose
private int count;
@SerializedName("pages")
@Expose
private int pages;
@SerializedName("tag")
@Expose
private Tag tag;
@SerializedName("posts")
@Expose
private List<Post> posts = null;

public String getStatus() {
    return status;
}

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

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public int getPages() {
    return pages;
}

public void setPages(int pages) {
    this.pages = pages;
}

public Tag getTag() {
    return tag;
}

public void setTag(Tag tag) {
    this.tag = tag;
}

public List<Post> getPosts() {
    return posts;
}

public void setPosts(List<Post> posts) {
    this.posts = posts;
}

}

Post.java

    package com.example;

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

public class Post {

@SerializedName("id")
@Expose
private int id;
@SerializedName("type")
@Expose
private String type;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("url")
@Expose
private String url;
@SerializedName("status")
@Expose
private String status;
@SerializedName("title")
@Expose
private String title;
@SerializedName("title_plain")
@Expose
private String titlePlain;
@SerializedName("content")
@Expose
private String content;
@SerializedName("excerpt")
@Expose
private String excerpt;
@SerializedName("date")
@Expose
private String date;
@SerializedName("modified")
@Expose
private String modified;
@SerializedName("categories")
@Expose
private List<Object> categories = null;
@SerializedName("tags")
@Expose
private List<Tag_> tags = null;
@SerializedName("author")
@Expose
private Author author;
@SerializedName("comments")
@Expose
private List<Object> comments = null;
@SerializedName("attachments")
@Expose
private List<Object> attachments = null;
@SerializedName("comment_count")
@Expose
private int commentCount;
@SerializedName("comment_status")
@Expose
private String commentStatus;
@SerializedName("custom_fields")
@Expose
private CustomFields customFields;

public int getId() {
    return id;
}

public void setId(int 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 getTitlePlain() {
    return titlePlain;
}

public void setTitlePlain(String titlePlain) {
    this.titlePlain = titlePlain;
}

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 List<Object> getCategories() {
    return categories;
}

public void setCategories(List<Object> categories) {
    this.categories = categories;
}

public List<Tag_> getTags() {
    return tags;
}

public void setTags(List<Tag_> tags) {
    this.tags = tags;
}

public Author getAuthor() {
    return author;
}

public void setAuthor(Author author) {
    this.author = author;
}

public List<Object> getComments() {
    return comments;
}

public void setComments(List<Object> comments) {
    this.comments = comments;
}

public List<Object> getAttachments() {
    return attachments;
}

public void setAttachments(List<Object> attachments) {
    this.attachments = attachments;
}

public int getCommentCount() {
    return commentCount;
}

public void setCommentCount(int commentCount) {
    this.commentCount = commentCount;
}

public String getCommentStatus() {
    return commentStatus;
}

public void setCommentStatus(String commentStatus) {
    this.commentStatus = commentStatus;
}

public CustomFields getCustomFields() {
    return customFields;
}

public void setCustomFields(CustomFields customFields) {
    this.customFields = customFields;
}

}

Tag.java

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

public class Tag {

@SerializedName("id")
@Expose
private int id;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("post_count")
@Expose
private int postCount;

public int getId() {
    return id;
}

public void setId(int 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 int getPostCount() {
    return postCount;
}

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

}

Tag_.java

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

public class Tag_ {

@SerializedName("id")
@Expose
private int id;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("post_count")
@Expose
private int postCount;

public int getId() {
    return id;
}

public void setId(int 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 int getPostCount() {
    return postCount;
}

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

}
0
On

That is because you're telling Gson to parse and give you an object that doesn't exist in your response. You can't just expect it to magically work ;)

For more info you can go through this offical guide of Gson