My api response is like:
"stories": [
{
"id": 3509,
"title": "Greece financial crises ",
"isBreaking": "0",
"updated": "1435719565",
"readers": "0",
"feed": [
{
"feed_id": "2",
"title": "cnn",
"logo_url": "http:\\/\\/cnn.com\\/images\\/logo.gif",
"site_url": "http:\\/\\/cnn.com\\/news.php\\/"
}
]
},
For that, i have created two classes. The first one is StoryAbstact
class which has field feed
of type Feeds
class
public class StoryAbstract {
private long id;
private String title;
private int isBreaking;
private Long updated;
private long readers;
private String date;
private String image;
private List<Feeds> feed;
..
// getters and getters
public List<Feeds> getFeed() {
return feed;
}
public void setFeed(List<Feeds> feed) {
this.feed = feed;
}
....
....
The second class is Feeds
like:
public class Feeds {
private int feed_id;
private String title;
private String logo_url;
private String site_url;
..
// getters & setters
public int getFeed_id() {
return feed_id;
}
public void setFeed_id(int feed_id) {
this.feed_id = feed_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLogo_url() {
return logo_url;
}
public void setLogo_url(String logo_url) {
this.logo_url = logo_url;
}
public String getSite_url() {
return site_url;
}
public void setSite_url(String site_url) {
this.site_url = site_url;
}
//setters
}
I want to show information from both classes ( StoryAbstract
and Feeds
) in my recyclerView adapter and for that, i am able to get the data deserialized from first class but only null from the second class.
i think this is due to that, the second class is not deserialized and i am not able to solve it. i am using Retrofit 1.9.0.
Any help please...
I have followed the answers below but none of them have solved my problem. i believe i have some thing wrong when i call the Feed class in my adapter. my adapter code is like:
private static class ViewHolderItem extends RecyclerView.ViewHolder implements View.OnClickListener {
private final TextView txtTitle;
private final ImageView imageIcon;
private final TextView feedName;
private Context context;
private StoryAbstract story;
private TextView timeStamp;
private Feeds feed;
public ViewHolderItem(View itemView, Context context) {
super(itemView);
this.context = context;
txtTitle = (TextView) itemView.findViewById(R.id.list_item_title);
imageIcon = (ImageView) itemView.findViewById(R.id.list_item_image);
feedName = (TextView) itemView.findViewById(R.id.feedName);
itemView.setOnClickListener(this);
}
public void bindStory(StoryAbstract story) {
this.story = story;
txtTitle.setText(story.getTitle());
feedName.setText(feed.getTitle());
String imageUrl = story.getImageUrl();
if (!TextUtils.isEmpty(imageUrl)) {
Ion.with(imageIcon)
.placeholder(R.drawable.loading)
.error(R.drawable.loading)
.load(imageUrl);
imageIcon.setVisibility(View.VISIBLE);
} else {
imageIcon.setVisibility(View.GONE);
}
}
feedName.setText(feed.getTitle());
returns null
Try this:
public class JsonModel implements Serializable {
private static final long serialVersionUID = -6186015239095323524L;
}
public class Feed extends JsonModel {
private static final long serialVersionUID = -8152796378433288199L;
@SerializedName("feed_id")
private int feedId;
@SerializedName("title")
private String title;
@SerializedName("logo_url")
private String logoUrl;
@SerializedName("site_url")
private String siteUrl;
public int getFeedId() {
return feedId;
}
public void setFeedId(int feedId) {
this.feedId = feedId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
public String getSiteUrl() {
return siteUrl;
}
public void setSiteUrl(String siteUrl) {
this.siteUrl = siteUrl;
}
}
public class StoryAbstract extends JsonModel{
private static final long serialVersionUID = 1269037550260848268L;
@SerializedName("id")
private long id;
@SerializedName("title")
private String title;
@SerializedName("isBreaking")
private int isBreaking;
@SerializedName("updated")
private long updated;
@SerializedName("readers")
private long readers;
@SerializedName("feed")
private Feed feed;
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;
}
public int getIsBreaking() {
return isBreaking;
}
public void setIsBreaking(int isBreaking) {
this.isBreaking = isBreaking;
}
public long getUpdated() {
return updated;
}
public void setUpdated(long updated) {
this.updated = updated;
}
public long getReaders() {
return readers;
}
public void setReaders(long readers) {
this.readers = readers;
}
public Feed getFeed() {
return feed;
}
public void setFeed(Feed feed) {
this.feed = feed;
}
}
And finally:
public class StoriesResponse extends JsonModel{
private static final long serialVersionUID = -2329376390030680204L;
@SerializedName("stories")
private List<StoryAbstract> storyAbstracts;
public List<StoryAbstract> getStoryAbstracts() {
return storyAbstracts;
}
public void setStoryAbstracts(List<StoryAbstract> storyAbstracts) {
this.storyAbstracts = storyAbstracts;
}
}
Just add StoriesResponse in your Retrofit Callback.
Im not be able to reproduce your error, but I have created an example using your json response, and this approach is working fine:
JSON
private static String JSON = "{\"stories\":[{\"id\":3509,\"title\":\"Greecefinancialcrises\",\"isBreaking\":\"0\",\"updated\":\"1435719565\",\"readers\":\"0\",\"feed\":[{\"feed_id\":\"2\",\"title\":\"cnn\",\"logo_url\":\"http:\\\\/\\\\/cnn.com\\\\/images\\\\/logo.gif\",\"site_url\":\"http:\\\\/\\\\/cnn.com\\\\/news.php\\\\/\"}]}]}";
StoriesResponse class
public static class StoriesResponse {
private List<Story> stories;
public List<Story> getStories() {
return stories;
}
@Override
public String toString() {
return "StoriesResponse{" +
"stories=" + stories +
'}';
}
}
Story class
public static class Story {
private long id;
private String title;
private int isBreaking;
private long updated;
private int readers;
private List<Feed> feed;
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public int getIsBreaking() {
return isBreaking;
}
public long getUpdated() {
return updated;
}
public int getReaders() {
return readers;
}
public List<Feed> getFeed() {
return feed;
}
@Override
public String toString() {
return "Story{" +
"id=" + id +
", title='" + title + '\'' +
", isBreaking=" + isBreaking +
", updated=" + updated +
", readers=" + readers +
", feed=" + feed +
'}';
}
}
Feed class
public static class Feed {
private long feed_id;
private String title;
private String logo_url;
private String site_url;
public long getFeed_id() {
return feed_id;
}
public String getTitle() {
return title;
}
public String getLogo_url() {
return logo_url;
}
public String getSite_url() {
return site_url;
}
@Override
public String toString() {
return "Feed{" +
"feed_id=" + feed_id +
", title='" + title + '\'' +
", logo_url='" + logo_url + '\'' +
", site_url='" + site_url + '\'' +
'}';
}
}
Main method to test it:
Gson gson = new Gson();
StoriesResponse response = gson.fromJson(JSON,StoriesResponse.class);
LOG(response);
The response given by toString()
implementation is:
StoriesResponse{stories=[Story{id=3509, title='Greecefinancialcrises', isBreaking=0, updated=1435719565, readers=0, feed=[Feed{feed_id=2, title='cnn', logo_url='http:\/\/cnn.com\/images\/logo.gif', site_url='http:\/\/cnn.com\/news.php\/'}]}]}
They are all static classes because I've opened a new project to test this.
This is not a solution, but I have reproduced the steps that Gson do within retrofit lib.
I always use a kind of a wrapper to wrap the responses (like StoriesResponse), because I usually need to check some response code.
Hope it helps.
Cheers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.