我的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
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适配器中显示来自两个类(StoryAbstract
和Feeds
)的信息,为此,我能够从第一个类中反序列化数据,但是从第二个类中只能返回null
我认为这是由于这一点,第二类没有反序列化,我无法解决它。我正在使用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
答案 0 :(得分:1)
试试这个:
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 Callback中添加StoriesResponse。
答案 1 :(得分:0)
我无法重现您的错误,但我使用您的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 +
'}';
}
}
故事课
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类
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 + '\'' +
'}';
}
}
测试它的主要方法:
Gson gson = new Gson();
StoriesResponse response = gson.fromJson(JSON,StoriesResponse.class);
LOG(response);
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\/'}]}]}
它们都是静态类,因为我已经打开了一个新项目来测试它。
这不是一个解决方案,但我已经重现了Gson在改造lib中所做的步骤。
我总是使用一种包装来包装响应(比如StoriesResponse),因为我通常需要检查一些响应代码。
希望它有所帮助。
干杯