【问题标题】:Android volley: Expected BEGIN_ARRAY but was BEGIN_OBJECTAndroid volley:应为 BEGIN_ARRAY,但为 BEGIN_OBJECT
【发布时间】:2017-05-22 05:30:14
【问题描述】:

我正在开发 Android 应用程序,我需要从 Wordpress 博客(带有特定标签)获取帖子。 JSON API 插件已安装:https://wordpress.org/plugins/json-api/

在我的应用程序中,我使用 Volley 库。我收到此错误:

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

我的代码:

 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);

有人可以帮助我并告诉我需要更改哪些内容才能使其正常工作吗?

【问题讨论】:

  • 发布您的 JSON 响应
  • 您显然有一个 json object 但正试图将其解析为 json array
  • 你必须创建一个模型类。
  • 发生异常是因为 fromJson 方法需要 List,因为您已经传递了 List.class 并且响应以 JsonObject 而不是 List 开头。所以首先你必须解析一个 JsonObject。

标签: android json android-studio android-volley json-api


【解决方案1】:

试试这个:

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

而不是..

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

【讨论】:

    【解决方案2】:

    使用这个模型类 模型类使用JsonSchema2POJO 创建。

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

    完整的 Json 会解析成 Java 对象,并使用 java 对象来获取数据。

    作者.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;
    }
    
    }
    

    标签.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;
    }
    
    }
    

    【讨论】:

      【解决方案3】:

      那是因为您告诉Gson 解析并给您一个响应中不存在的对象。你不能指望它会神奇地工作;)

      欲了解更多信息,您可以阅读Gson的官方指南

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-12
        • 2018-04-24
        • 2016-12-27
        • 2013-08-10
        • 1970-01-01
        • 2017-10-16
        相关资源
        最近更新 更多