gpt4 book ai didi

java - Retrofit 无法正确解析 body

转载 作者:行者123 更新时间:2023-11-29 02:34:55 25 4
gpt4 key购买 nike

我无法使用 Retrofit2 + Gson 正确解析我的 Web 服务的响应。我已经实现了类似的响应,并且它们都可以正常工作。当响应主体返回一个自定义对象数组时,就会出现问题。

我已经阅读了来自 stackoverflow 的许多回复,并且我使用 http://www.jsonschema2pojo.org/ 制作了我的类(class)但没办法...

我粘贴了我所有的代码。希望你能帮我。谢谢!

网络服务响应

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

retrofit 客户端

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

最佳答案

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

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

与:

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

关于java - Retrofit 无法正确解析 body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47629413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com