简体   繁体   中英

How to Handle different JSON response types from same endpoint in RetroFit 2

This is my json api :- https://test324y.highape.com/api/get_homepage_data?token=xyz123

As you can see it has many objects with different types. When i paste it to http://www.jsonschema2pojo.org/ it gives me multiple class .

I want to know how do i set data to those model class using retrofit from same end point ?

I'd suggest using only one model class for parsing the response using Retrofit.

Example: Create a model class say Response.java which contains following model classes:

  1. FeaturedEvents
  2. TrendingEvents
  3. PopularActivities
  4. Offers
  5. Venues
  6. Artists

All above model classes contain respective data. This way you can easily parse the response and If API excludes any model in response, then parsed value for that model will be null . You should always check it before showing it in UI.

Hope this will help!

after generating Pojos from http://www.jsonschema2pojo.org/
you will get a class for every object in the response. but you will only use the class that's called Example with retrofit. and later you can access all response value through it.

And of course rename your response class to something more related to the endpoint. eg "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;
}

}

your endpoint will be something like this.

@GET("get_homepage_data")
Call<Example> getExample(@Query("token") String token);

Endpoint call example

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM