1

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?

7
  • I have a doubt - shows variable is the list of Show object. Why you are calling shows.rating.percentage, rating is part of individual Show object, not the entire list. Commented Jul 14, 2014 at 6:44
  • nice catch. i edited it. this part "ratings": {"percentage": 82, "votes": 8377, "loved": 7629, "hated": 748}, for some reasons retrofit didn't convert it to json. Commented Jul 14, 2014 at 6:55
  • The rating field in Show class should be ratings to 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. Commented Jul 15, 2014 at 8:28
  • @HassanIbraheem you're right. should be Ratings ratings; as previewed by jsonschema2pojo and I edited it but still Show.ratings is null. I also followed Roman Nurik's muzei source on Github. Commented Jul 16, 2014 at 2:02
  • @chip did you manage to solve this? i am kinda stuck on same issue! Commented Dec 27, 2014 at 15:23

2 Answers 2

3

Retrofit uses Gson by default so this should fix your problem.

import com.google.gson.annotations.Expose;

public class Show {

@Expose
private String title;
@Expose
private Integer year;
@Expose
private Integer watchers;
@Expose
private Ratings ratings;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Integer getYear() {
    return year;
}

public void setYear(Integer year) {
    this.year = year;
}

public Integer getWatchers() {
    return watchers;
}

public void setWatchers(Integer watchers) {
    this.watchers = watchers;
}

public Ratings getRatings() {
    return ratings;
}

public void setRatings(Ratings ratings) {
    this.ratings = ratings;
}

}

And the Ratings class

import com.google.gson.annotations.Expose;

public class Ratings {

@Expose
private Integer percentage;
@Expose
private Integer votes;
@Expose
private Integer loved;
@Expose
private Integer hated;

public Integer getPercentage() {
    return percentage;
}

public void setPercentage(Integer percentage) {
    this.percentage = percentage;
}

public Integer getVotes() {
    return votes;
}

public void setVotes(Integer votes) {
    this.votes = votes;
}

public Integer getLoved() {
    return loved;
}

public void setLoved(Integer loved) {
    this.loved = loved;
}

public Integer getHated() {
    return hated;
}

public void setHated(Integer hated) {
    this.hated = hated;
}

}

Let me know if that helped.

Sign up to request clarification or add additional context in comments.

Comments

0

How about using with annotations, like below:

@JsonPropertyOrder({
"title",
"year",
"watchers",
"ratings"
})
public class Example {

@JsonProperty("title")
private String title;
@JsonProperty("year")
private Integer year;
@JsonProperty("watchers")
private Integer watchers;
@JsonProperty("ratings")
private Ratings ratings;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("title")
public String getTitle() {
return title;
}

@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}

@JsonProperty("year")
public Integer getYear() {
return year;
}

@JsonProperty("year")
public void setYear(Integer year) {
this.year = year;
}

@JsonProperty("watchers")
public Integer getWatchers() {
return watchers;
}

@JsonProperty("watchers")
public void setWatchers(Integer watchers) {
this.watchers = watchers;
}

@JsonProperty("ratings")
public Ratings getRatings() {
return ratings;
}

@JsonProperty("ratings")
public void setRatings(Ratings ratings) {
this.ratings = ratings;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Ratings.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"percentage",
"votes",
"loved",
"hated"
})
public class Ratings {

@JsonProperty("percentage")
private Integer percentage;
@JsonProperty("votes")
private Integer votes;
@JsonProperty("loved")
private Integer loved;
@JsonProperty("hated")
private Integer hated;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("percentage")
public Integer getPercentage() {
return percentage;
}

@JsonProperty("percentage")
public void setPercentage(Integer percentage) {
this.percentage = percentage;
}

@JsonProperty("votes")
public Integer getVotes() {
return votes;
}

@JsonProperty("votes")
public void setVotes(Integer votes) {
this.votes = votes;
}

@JsonProperty("loved")
public Integer getLoved() {
return loved;
}

@JsonProperty("loved")
public void setLoved(Integer loved) {
this.loved = loved;
}

@JsonProperty("hated")
public Integer getHated() {
return hated;
}

@JsonProperty("hated")
public void setHated(Integer hated) {
this.hated = hated;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

1 Comment

tried this tool. i'm not sure why AS version 0.8.2 cannot find javax.annotation.Generated; and java version 1.7.0_60

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.