gpt4 book ai didi

java - 使用 GSON 和 Retrofit 时反序列化内部类

转载 作者:行者123 更新时间:2023-12-01 11:21:45 25 4
gpt4 key购买 nike

我的 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\\/"
}
]
},

为此,我创建了两个类。第一个是 StoryAbstact 类,其字段 feed 类型为 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;
}

....
....

第二个类是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
}

我想在我的 recyclerView 适配器中显示来自两个类(StoryAbstractFeeds)的信息,为此,我能够从第一类获取反序列化的数据,但是仅第二类为空。

我认为这是由于第二类没有反序列化,我无法解决它。我正在使用 Retrofit 1.9.0。

请帮忙...

编辑

我已遵循以下答案,但没有一个能解决我的问题。我相信当我在适配器中调用 Feed 类时出现了一些问题。我的适配器代码如下:

 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()); 返回 null

最佳答案

试试这个:

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;
}

}

最后:

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;
}

}

只需在您的 Retrofit 回调中添加 StoriesResponse 即可。

关于java - 使用 GSON 和 Retrofit 时反序列化内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com