Reputation: 361
I'm having a lot of trouble trying to get Retrofit to parse my JSON, I can't wrap my head around. Whenever I run my Retrofit code I get a crash:
Caused by: java.lang.StackOverflowError: stack size 1036KB
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.<init>($Gson$Types.java:537)
and then later on:
E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!
My JSON looks like this:
{
"199" : {
"anncontent" : "content",
"anndisplaydate" : "Tuesday February 11",
"announcements_id" : "199",
"anntitle" : "Newsletter Issue 17"
},
"264" : {
"anncontent" : "content",
"anndate" : "2014-10-03 22:37:32",
"anndisplaydate" : "Friday October 03",
"announcements_id" : "264",
"anntitle" : "Timetable"
},
"267" : {
"anncontent" : "content",
"anndate" : "2014-10-10 02:41:32",
"anndisplaydate" : "Friday October 10",
"announcements_id" : "267",
"anntitle" : "Author talk with Hugh O’Brien"
}
}
My announcement class fields look like this:
private String anntitle;
private String anncontent;
private String anndisplaydate;
private Date anndate;
private String announcements_id;
My interface looks like this:
public interface AnnouncementsAPI {
@GET("/announcements.json")
ArrayList<Announcement> listAnnouncements();}
And finally, my Retrofit code looks like this:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://MYURL")
.build();
AnnouncementsAPI announcementsAPI = restAdapter.create(AnnouncementsAPI.class);
ArrayList<Announcement> announcements = announcementsAPI.listAnnouncements();
Log.d("RETROFIT", "Announcemnts: " + announcements.toString());
I'm just trying to get an ArrayList of Announcement objects, and I'm not sure how to go about it. Sorry if this has been asked before, but all the previous questions/examples I found used JSON that seemed drastically different to mine.
If anyone has any ideas it'd be much appreciated. Thanks
Upvotes: 2
Views: 1326
Reputation: 1112
If all object extends same base object, then StackOverflowError: stack size 1036KB exception occurs.
example:
class BaseBean
class A extends BaseBean
(class A
contains reference of class A
and class B
)
class B extends BaseBean
class C extends BaseBean
Implementing above case, there are big possibility for occurring same exception. (don't extend with BaseBean).
Upvotes: 2
Reputation: 9
Try this. Response should be in this format HashMap<String,Announcement>
Upvotes: -1
Reputation: 1632
Try using a retrofit callback
for your interface, like this :
The interface
public interface AnnouncementsAPI {
@GET("/announcements.json")
void listAnnouncements(Callback<ListAnnouncement> callback);
}
If you want the return to be a List, Encapsulate the announcement class into another class like so :
ListAnnouncement.Java
public class ListAnnouncement {
private List<Announcement> fieldname; //match the fieldname with the field name on the JSON
//add getter-setter as needed
}
The Retrofit Code
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://MYURL")
.build();
AnnouncementsAPI announcementsAPI = restAdapter.create(AnnouncementsAPI.class);
final ListAnnouncement listAnnouncement;
announcementsAPI.listAnnouncements(new Callback<ListAnnouncement>() {
@Override
public void success(ListAnnouncement data, Response response) {
listAnnouncement = data;
}
@Override
public void failure(RetrofitError error) {
}
}
);
After that, you can use the fieldname
on the ListAnnouncement
Class
to get the list of Announcement
.
Important
The code above will work if your JSON is indeed a JSON Array
, by the looks of it, you are getting returns of several Announcement
objects with different key, which is not a JSON Array
Upvotes: 1