2014-09-01 51 views
0

是BEGIN_OBJECT這是JSON氟里昂http://api.ihackernews.com/page改造無法解析JSON:BEGIN_ARRAY但hackernews API

{ 
    nextId: "2", 
    items: [ 
     { 
      title: "How I Start: Go", 
      url: "http://howistart.org/posts/go/1", 
      id: 8254143, 
      commentCount: 2, 
      points: 41, 
      postedAgo: "57 minutes ago", 
      postedBy: "dokuda" 
     }, 
     { 
      title: "Plants in offices increase happiness and productivity", 
      url: "http://www.theguardian.com/money/2014/aug/31/plants-offices-workers-productive-       minimalist-employees", 
      id: 8253979, 
      commentCount: 20, 
      points: 60, 
      postedAgo: "1 hour ago", 
      postedBy: "dsr12" 
     } 
    ] 
}  

與www.jsonschema2pojo.org

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder({ 
    "title", 
    "url", 
    "id", 
    "commentCount", 
    "points", 
    "postedAgo", 
    "postedBy" 
}) 
public class Article { 

@JsonProperty("title") 
private String title; 
@JsonProperty("url") 
private String url; 
@JsonProperty("id") 
private Integer id; 
@JsonProperty("commentCount") 
private Integer commentCount; 
@JsonProperty("points") 
private Integer points; 
@JsonProperty("postedAgo") 
private String postedAgo; 
@JsonProperty("postedBy") 
private String postedBy; 
private String image; 
@JsonIgnore 
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 


public String getImage() {return image;} 
public void setImage(String image){this.image = image;} 
@JsonProperty("title") 
public String getTitle() { 
    return title; 
} 

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

public Article withTitle(String title) { 
    this.title = title; 
    return this; 
} 

@JsonProperty("url") 
public String getUrl() { 
    return url; 
} 

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

public Article withUrl(String url) { 
    this.url = url; 
    return this; 
} 

@JsonProperty("id") 
public Integer getId() { 
    return id; 
} 

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

public Article withId(Integer id) { 
    this.id = id; 
    return this; 
} 

@JsonProperty("commentCount") 
public Integer getCommentCount() { 
    return commentCount; 
} 

@JsonProperty("commentCount") 
public void setCommentCount(Integer commentCount) { 
    this.commentCount = commentCount; 
} 

public Article withCommentCount(Integer commentCount) { 
    this.commentCount = commentCount; 
    return this; 
} 

@JsonProperty("points") 
public Integer getPoints() { 
    return points; 
} 

@JsonProperty("points") 
public void setPoints(Integer points) { 
    this.points = points; 
} 

public Article withPoints(Integer points) { 
    this.points = points; 
    return this; 
} 

@JsonProperty("postedAgo") 
public String getPostedAgo() { 
    return postedAgo; 
} 

@JsonProperty("postedAgo") 
public void setPostedAgo(String postedAgo) { 
    this.postedAgo = postedAgo; 
} 

public Article withPostedAgo(String postedAgo) { 
    this.postedAgo = postedAgo; 
    return this; 
} 

@JsonProperty("postedBy") 
public String getPostedBy() { 
    return postedBy; 
} 

@JsonProperty("postedBy") 
public void setPostedBy(String postedBy) { 
    this.postedBy = postedBy; 
} 

public Article withPostedBy(String postedBy) { 
    this.postedBy = postedBy; 
    return this; 
} 

@Override 
public String toString() { 
    return ToStringBuilder.reflectionToString(this); 
} 

@Override 
public int hashCode() { 
    return HashCodeBuilder.reflectionHashCode(this); 
} 

@Override 
public boolean equals(Object other) { 
    return EqualsBuilder.reflectionEquals(this, other); 
} 

@JsonAnyGetter 
public Map<String, Object> getAdditionalProperties() { 
    return this.additionalProperties; 
} 

@JsonAnySetter 
public void setAdditionalProperty(String name, Object value) { 
    this.additionalProperties.put(name, value); 
} 

public Article withAdditionalProperty(String name, Object value) { 
    this.additionalProperties.put(name, value); 
    return this; 
} 

} 
創建這是我的代碼

POJO條

ApiClient public class ApiClient public interface UnlockApiInterface { { public interface UnlockApiInterface { @GET(「/ page」) void getArticles(Callback> callback);

} 

private static UnlockApiInterface unlockService; 

public static UnlockApiInterface getUnlockApiClient(){ 
    if(unlockService==null){ 
     RestAdapter restAdapter = new RestAdapter.Builder() 
       .setLogLevel(RestAdapter.LogLevel.FULL) 
       .setEndpoint("http://api.ihackernews.com") 
       .build(); 
     unlockService = restAdapter.create(UnlockApiInterface.class); 
    } 
    return unlockService; 
} 

,我得到這個

ApiClient.getUnlockApiClient().getArticles(new Callback<List<Article>>(){ 
     @Override 
     public void success(List<Article> articles, Response response) { 
      for (Article article : articles) { 
       Log.i(TAG,article.toJSON()); 
      } 

     } 

JSON的,但我得到這個錯誤

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 

任何想法如何解決這一問題?

回答

0

您試圖解析項目數組,但響應只是一個對象。所以爲了讓它工作,你可以創建一個新的類Response,它將有兩個字段nextIdList<Article>,爲他們提供適當的註釋,並希望這應該可以做到。

之後,您可以從Response對象中檢索Article的列表。