2

I am not being able to parse correctly the response of my web service using Retrofit2 + Gson. I have implemented similar responses and all of them work correctly. The problem occurs when the response body returns an array of custom objects.

I have read many responses from stackoverflow and I made my classes using http://www.jsonschema2pojo.org/ but no way...

I paste all my code. Hope you can help me. Thanks!

Webservice response

    {
    "code": "SUCCESS",
    "errorMessage": null,
    "data": [
        {
            "nickname": "David",
            "tiempoMiliseg": 49745,
            "fecha": "2017-07-14T13:59:12.000Z"
        },
        {
            "nickname": "kart 13",
            "tiempoMiliseg": 54034,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "kart 26",
            "tiempoMiliseg": 54728,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "kart 16",
            "tiempoMiliseg": 54845,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "jonathan",
            "tiempoMiliseg": 55061,
            "fecha": "2017-07-14T19:12:57.000Z"
        },
        {
            "nickname": "kart 20",
            "tiempoMiliseg": 55187,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "kart 5",
            "tiempoMiliseg": 55584,
            "fecha": "2017-07-14T15:51:53.000Z"
        },
        {
            "nickname": "kart 5",
            "tiempoMiliseg": 55926,
            "fecha": "2017-07-14T15:21:30.000Z"
        },
        {
            "nickname": "kart 19",
            "tiempoMiliseg": 56025,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 11",
            "tiempoMiliseg": 56403,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 22",
            "tiempoMiliseg": 56458,
            "fecha": "2017-07-14T15:51:53.000Z"
        },
        {
            "nickname": "kart 28",
            "tiempoMiliseg": 56467,
            "fecha": "2017-07-14T18:10:08.000Z"
        },
        {
            "nickname": "kart 2",
            "tiempoMiliseg": 56548,
            "fecha": "2017-07-14T18:10:08.000Z"
        },
        {
            "nickname": "kart 27",
            "tiempoMiliseg": 56620,
            "fecha": "2017-07-14T15:51:53.000Z"
        },
        {
            "nickname": "kart 27",
            "tiempoMiliseg": 56629,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 20",
            "tiempoMiliseg": 56827,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 26",
            "tiempoMiliseg": 56846,
            "fecha": "2017-07-14T18:56:31.000Z"
        },
        {
            "nickname": "kart 28",
            "tiempoMiliseg": 57071,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 26",
            "tiempoMiliseg": 57088,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 29",
            "tiempoMiliseg": 57143,
            "fecha": "2017-07-14T18:34:01.000Z"
        }
    ]
}

Retrofit Response class

public class RankingResponse {

@SerializedName("code")
@Expose
private String code;
@SerializedName("errorMessage")
@Expose
private Object errorMessage;
@SerializedName("data")
@Expose
private List<Datum> data = null;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public Object getErrorMessage() {
    return errorMessage;
}

public void setErrorMessage(Object errorMessage) {
    this.errorMessage = errorMessage;
}

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

@Override
public String toString() {
    return "RankingResponse{" +
            "code='" + code + '\'' +
            ", errorMessage=" + errorMessage +
            ", data=" + data +
            '}';
}


public class Datum {
    @SerializedName("nickname")
    @Expose
    private String nickname;
    @SerializedName("tiempoMiliseg")
    @Expose
    private Integer tiempoMiliseg;
    @SerializedName("fecha")
    @Expose
    private String fecha;

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Integer getTiempoMiliseg() {
        return tiempoMiliseg;
    }

    public void setTiempoMiliseg(Integer tiempoMiliseg) {
        this.tiempoMiliseg = tiempoMiliseg;
    }

    public String getFecha() {
        return fecha;
    }

    public void setFecha(String fecha) {
        this.fecha = fecha;
    }

    @Override
    public String toString() {
        return "Datum{" +
                "nickname='" + nickname + '\'' +
                ", tiempoMiliseg=" + tiempoMiliseg +
                ", fecha='" + fecha + '\'' +
                '}';
    }
}

}

Retrofit client

public class RetrofitClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

Retrofit response

enter image description here

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
Marta Tenés
  • 2,102
  • 1
  • 13
  • 22

1 Answers1

0

In your RankingResponse class replace following:

@SerializedName("data")
@Expose
private List<Datum> data = null;

with:

@SerializedName("data")
private List<Datum> data =  new ArrayList<Datum>();
Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21