改造无法正确解析身体

时间:2017-12-04 08:41:29

标签: java android gson retrofit retrofit2

我无法使用Retrofit2 + Gson正确解析我的Web服务的响应。 我已经实现了类似的响应,所有这些响应都正常。响应主体返回自定义对象数组时会发生此问题。

我已经阅读了很多来自stackoverflow的回复,并且我使用http://www.jsonschema2pojo.org/创建了我的课程但是没办法...

我粘贴了所有代码。希望您能够帮助我。谢谢!

网络服务响应

    {
    "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"
        }
    ]
}

改造响应类

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 + '\'' +
                '}';
    }
}

}

改造客户

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

改进回复

enter image description here

1 个答案:

答案 0 :(得分:0)

在您的RankingResponse类中替换以下内容:

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

使用:

@SerializedName("data")
private List<Datum> data =  new ArrayList<Datum>();