[英]Deserializing inner class when using GSON and Retrofit
My api response is like: 我的api响应类似于:
"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 第一个是
StoryAbstact
类,它具有Feeds
类类型的字段feed
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: 第二类是
Feeds
例如:
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. 我想在我的recyclerView适配器中显示两个类(
StoryAbstract
和Feeds
)的信息,为此,我能够从第一类中反序列化数据,但从第二类中仅获取空值。
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.
我正在使用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.
我相信在适配器中调用Feed类时出现了一些问题。 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 返回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. 只需在改造回调中添加StoriesResponse。
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响应创建了一个示例,并且这种方法可以正常工作:
JSON 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: toString()
实现给出的响应是:
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. 这不是解决方案,但我已复制了Gson在改造lib中所做的步骤。
I always use a kind of a wrapper to wrap the responses (like StoriesResponse), because I usually need to check some response code. 我总是使用一种包装器来包装响应(例如StoriesResponse),因为我通常需要检查一些响应代码。
Hope it helps. 希望能帮助到你。
Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.