这是我的json :- data?token=xyz123
如您所见,它有许多不同类型的对象。当我将它粘贴到http://www.jsonschema2pojo.org/时,它会给我多个类。
我想知道如何使用来自同一端点的修改将数据设置到那些模型类?
发布于 2018-04-19 01:32:21
从http://www.jsonschema2pojo.org/生成Pojos之后
您将为响应中的每个对象获得一个类。但是,您将只使用名为样例的类进行改造。稍后,您可以通过它访问所有响应值。
当然,将响应类重命名为与端点更相关的内容。例如"HomePageResponse"
public class Example {
@SerializedName("featured_events")
@Expose
private FeaturedEvents featuredEvents;
@SerializedName("trending_events")
@Expose
private TrendingEvents trendingEvents;
@SerializedName("popular_activities")
@Expose
private PopularActivities popularActivities;
@SerializedName("event_count")
@Expose
private EventCount eventCount;
@SerializedName("offers")
@Expose
private Offers offers;
@SerializedName("venues")
@Expose
private Venues venues;
@SerializedName("artists")
@Expose
private Artists artists;
public FeaturedEvents getFeaturedEvents() {
return featuredEvents;
}
public void setFeaturedEvents(FeaturedEvents featuredEvents) {
this.featuredEvents = featuredEvents;
}
public TrendingEvents getTrendingEvents() {
return trendingEvents;
}
public void setTrendingEvents(TrendingEvents trendingEvents) {
this.trendingEvents = trendingEvents;
}
public PopularActivities getPopularActivities() {
return popularActivities;
}
public void setPopularActivities(PopularActivities popularActivities) {
this.popularActivities = popularActivities;
}
public EventCount getEventCount() {
return eventCount;
}
public void setEventCount(EventCount eventCount) {
this.eventCount = eventCount;
}
public Offers getOffers() {
return offers;
}
public void setOffers(Offers offers) {
this.offers = offers;
}
public Venues getVenues() {
return venues;
}
public void setVenues(Venues venues) {
this.venues = venues;
}
public Artists getArtists() {
return artists;
}
public void setArtists(Artists artists) {
this.artists = artists;
}
}
您的端点将是这样的。
@GET("get_homepage_data")
Call<Example> getExample(@Query("token") String token);
端点调用示例
Call<Example> call = api.getExample(token);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
Example example = response.body();
// use example object to get any value you want
}
@Override
public void onFailure(Call<HyperResponse> call, Throwable t) {
if (t != null && t.getMessage() != null)
Log.e("Failure", "Example Failure" + t.getMessage());
}
});
发布于 2018-04-19 01:31:16
我建议只使用一个模型类来解析响应。
示例:创建一个模型类,比如Response.java
,它包含以下模型类:
上述所有模型类都包含各自的数据。通过这种方式,您可以轻松地解析响应,如果API排除了响应中的任何模型,那么该模型的解析值将是null
。在UI中显示之前,您应该始终检查它。
希望这能帮上忙!
https://stackoverflow.com/questions/49917194
复制相似问题