I am playing with Retrofit. I have trouble when the json value is/are JSONObjects like the ratings key
[{
"title": "True Blood",
"year": 2008,
"watchers": 36,
"ratings": {
"percentage": 82,
"votes": 8377,
"loved": 7629,
"hated": 748
}
}
]
In jsonschema2pojo, it says I have to create ratings key into another class
static class Show {
String title;
int year;
Ratings ratings;
}
static class Ratings {
int percentage;
}
interface TrendingService {
@GET("/shows/trending.json/{yourApiKey}")
public List<Show> getTrendingShows(@Path("yourApiKey") String yourApiKey);
}
public void onCreate(Bundle savedInstanceState) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
final TrendingService service = restAdapter.create(TrendingService.class);
new AsyncTask<Void, Void, List<Show>>() {
@Override
protected List<Show> doInBackground(Void... params) {
return service.getTrendingShows("ApiKey");
}
protected void onPostExecute(List<Show> shows) {
super.onPostExecute(shows);
for (Show show : shows) {
Log.d("Show", String.format(
"%s %s %d", show.title, show.poster, show.ratings.percentage));
// NULL
}
}
}
}
but the show.ratings.percentage is always null. Is there something I miss here?
showsvariable is the list ofShowobject. Why you are callingshows.rating.percentage, rating is part of individualShowobject, not the entire list."ratings": {"percentage": 82, "votes": 8377, "loved": 7629, "hated": 748}, for some reasons retrofit didn't convert it to json.ratingfield inShowclass should beratingsto match the key of your JSON object. Gson library uses field names by default to match keys in JSON objects, but you can override it with @SerializedName("ratings") annotation on the field.Ratings ratings;as previewed by jsonschema2pojo and I edited it but stillShow.ratingsis null. I also followed Roman Nurik's muzei source on Github.