2014-09-01 1 views
0

와 BEGIN_OBJECT했다이 개조는 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 http://api.ihackernews.com/page

프론 JSON입니다

ApiClient 공용 클래스 ApiClient { 공용 인터페이스 UnlockApiInterface { @GET ("/ page") void getArticles (콜백> 콜백);

} 

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

항목 배열을 구문 분석하려고하지만 응답이 단지 개체 일뿐입니다. 따라서 작동 시키려면 두 개의 필드 nextIdList<Article>을 가진 새로운 클래스 Response을 만들 수 있습니다. 적절한 주석을 달아 주면 좋겠지 만 트릭을해야합니다.

Response 개체에서 Article의 목록을 검색 할 수 있습니다.

관련 문제