Reputation: 241
I already looked at a similar question here : null pointer exception-parsing json with gson android
I'm getting same error but my objects are slightly different and it fails only on android 2.2 but not android 2.3. Somme googling seems to looks like it's a known 2.2 bug fixed in 2.3. But maybe there is a workaround ?
So here is the code :
NewsContainer newsContainer = gson.fromJson(response, NewsContainer.class);
the json answer :
{"newsList":
{"group":
{"news":
{"news":
{"ranking":"1","id":"NEWS-33713","type":"Fnac","title":"LAURENT GERARD, GERARD COMME ...","subtitle":"THEATRE DES MATHURINS","preview":"http:\/\/www.fnacspectacles.com\/static\/0\/visuel\/grand\/215\/LAURENT-GERARD_2159024664536169906.jpg?1325241781000","details":"LAURENT GERARD, GERARD COMME ...\nDu: 24\/01\/2012 au 30\/06\/2012","address":"36, rue des Mathurins 75008 PARIS","url":"http:\/\/ad.zanox.com\/ppc\/?21135664C184852886&ULP=[[\/place-spectacle\/manifestation\/Seul-en-scene-LAURENT-GERARD--GERARD-COMME-----RARD.htm]]","start_date":"2012-01-24 00:00:00","poi": {"latitude":"48.8731960","longitude":"2.3257960"}
}
}
}
}
}
And here is my objects :
public class NewsContainer {
private NewsList newsList;
public NewsContainer() {
;
}
private static class NewsList {
List<NewsGroup> group;
}
public List<NewsGroup> getNewsList() {
return newsList.group;
}
public void setNewsList(List<NewsGroup> newsList) {
this.newsList.group = newsList;
}
}
public class NewsGroup implements Group{
private Long id;
private String title;
// @Exclude
private List<News> news;
public List<News> getNews() {
return news;
}
public NewsGroup() {
}
public void setNews(List<News> news) {
this.news = news;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public List<? extends Item> getItems() {
return news;
}
}
The is a lot more objects involved, but it would be too long to had it here. Ask if you need to see one of them.
Regards
Upvotes: 0
Views: 525
Reputation: 2181
In the Gson, we generally use List < T > when we're dealing with JSON arrays. From your JSON answer, I see that there are no arrays (only objects within objects). If your newslist contains MANY groups, then JSON should be like:
{"newsList":
"group" : [{"news": .... }, ... ]
}
Upvotes: 1