medy75
medy75

Reputation: 656

Retrofit cannot parse some fields using gson converter

I am trying to use retrofit in Android to get some data the from server. Everything works fine, but in response there are some missing fields. The plain JSON response looks fine to me, I think the model is good too. These fields are not something special - integers and Strings. I used http://www.jsonschema2pojo.org/ to create this model.

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

models/Bin.java

public class Bin {

@JsonProperty("id")
private Integer id;
@JsonProperty("firm_id")
private Integer firmId;
@JsonProperty("number")
private Integer number;
@JsonProperty("frequency")
private String frequency;
@JsonProperty("placed")
private String placed;
@JsonProperty("taken")
private String taken;
@JsonProperty("shredded")
private Object shredded;
@JsonProperty("collect_date")
private String collectDate;
@JsonProperty("created_at")
private String createdAt;
@JsonProperty("updated_at")
private String updatedAt;
@JsonProperty("url")
private String url;

@JsonProperty("id")
public Integer getId() {
    return id;
}

@JsonProperty("id")
public void setId(Integer id) {
    this.id = id;
}

@JsonProperty("firm_id")
public Integer getFirmId() {
    return firmId;
}

@JsonProperty("firm_id")
public void setFirmId(Integer firmId) {
    this.firmId = firmId;
}

@JsonProperty("number")
public Integer getNumber() {
    return number;
}

@JsonProperty("number")
public void setNumber(Integer number) {
    this.number = number;
}

@JsonProperty("frequency")
public String getFrequency() {
    return frequency;
}

@JsonProperty("frequency")
public void setFrequency(String frequency) {
    this.frequency = frequency;
}

@JsonProperty("placed")
public String getPlaced() {
    return placed;
}

@JsonProperty("placed")
public void setPlaced(String placed) {
    this.placed = placed;
}

@JsonProperty("taken")
public String getTaken() {
    return taken;
}

@JsonProperty("taken")
public void setTaken(String taken) {
    this.taken = taken;
}

@JsonProperty("shredded")
public Object getShredded() {
    return shredded;
}

@JsonProperty("shredded")
public void setShredded(Object shredded) {
    this.shredded = shredded;
}

@JsonProperty("collect_date")
public String getCollectDate() {
    return collectDate;
}

@JsonProperty("collect_date")
public void setCollectDate(String collectDate) {
    this.collectDate = collectDate;
}

@JsonProperty("created_at")
public String getCreatedAt() {
    return createdAt;
}

@JsonProperty("created_at")
public void setCreatedAt(String createdAt) {
    this.createdAt = createdAt;
}

@JsonProperty("updated_at")
public String getUpdatedAt() {
    return updatedAt;
}

@JsonProperty("updated_at")
public void setUpdatedAt(String updatedAt) {
    this.updatedAt = updatedAt;
}

@JsonProperty("url")
public String getUrl() {
    return url;
}

@JsonProperty("url")
public void setUrl(String url) {
    this.url = url;
}

}

Retrofit interface:

@GET("bins.json")
Call<List<Bin>> getBins();

It returns:

[{"id":4,"firm_id":2,"number":3,"frequency":"daily","placed":"2018-10-17T00:00:00.000Z","taken":"2018-10-17T00:00:00.000Z","shredded":null,"collect_date":"2018-10-18T00:00:00.000Z","created_at":"2018-10-17T13:41:41.245Z","updated_at":"2018-10-17T13:41:41.245Z","url":"https://server/bins/4.json"},{"id":1,"firm_id":1,"number":1,"frequency":"weekly","placed":"2018-10-15T00:00:00.000Z","taken":"2018-10-17T00:00:00.000Z","shredded":null,"collect_date":"2018-10-24T00:00:00.000Z","created_at":"2018-10-15T12:27:32.294Z","updated_at":"2018-10-17T13:41:06.381Z","url":"https://server/bins/1.json"},{"id":2,"firm_id":1,"number":2,"frequency":"monthly","placed":"2018-10-17T00:00:00.000Z","taken":"2018-10-17T00:00:00.000Z","shredded":null,"collect_date":"2018-11-16T00:00:00.000Z","created_at":"2018-10-17T10:18:46.172Z","updated_at":"2018-10-17T10:18:46.172Z","url":"https://server/bins/2.json"}]

But in response.body() some fields missing:

enter image description here

Upvotes: 0

Views: 901

Answers (1)

Macmist
Macmist

Reputation: 723

This is most likely because you are using the @JsonProperty annotation while retrofit is deserializing with Gson. I believe @JsonProperty annotation is used for the jackson library, for Gson the correct annotation will be @SerializedName

So try to replace every @JsonProperty("name") with the equivalent @SerializedName("name") and your missing field should be populated correctly

Edit: After checking the website you provided, they have a 'Gson' option under the 'annotation style' section which will generate your pojo the correct way :)

Upvotes: 1

Related Questions