2017-01-07 138 views
0

我正在開發Android應用程序,我需要從WordPress博客(具有特定標籤)的帖子。 JSON API插件已安裝:https://wordpress.org/plugins/json-api/Android排空:期望BEGIN_ARRAY,但是BEGIN_OBJECT

在我的應用程序中,我使用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); 

有人能幫助我,告訴我需要什麼,以使其發揮作用的變化?

+0

本官方指南發表您的JSON respnse – rafsanahmad007

+0

你顯然有一個JSON'object'但試圖解析它作爲json'數組'。 – injecteer

+1

你必須創建一個模型類。 –

回答

0

試試這個:

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

,而不是..

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

仍然收到相同的錯誤,在這個新行 –

+0

發佈logcat錯誤.. – rafsanahmad007

0

使用此模型類 模型類創建使用JsonSchema2POJO

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

完整的Json將解析成Java對象並使用java對象來獲取數據。

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

那是因爲你告訴Gson解析,給你的對象不存在在你的響應。你不能只是指望它神奇的工作;)

欲瞭解更多信息,你可以去通過Gson

相關問題