改造无法正确解析正文
Posted
技术标签:
【中文标题】改造无法正确解析正文【英文标题】:Retrofit can't parse body correctly 【发布时间】:2018-05-17 16:26:54 【问题描述】:我无法使用 Retrofit2 + Gson 正确解析我的 Web 服务的响应。 我已经实现了类似的响应,并且它们都可以正常工作。当响应正文返回自定义对象数组时会出现问题。
我已经阅读了许多来自 *** 的回复,并且我使用 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;
改造响应
【问题讨论】:
androidhive.info/2016/05/… 不确定是否应该同时使用公开和序列化名称,请参阅:***.com/questions/34752200/… 发送到服务器的请求数据是否正确,您是否使用与您看到响应的相同的请求数据? @StarterPack 我只是先尝试了仅使用暴露,然后仅使用了 SerializedName,但它们都没有工作 哈哈,我日常生活中的一个故事。乐意效劳。干杯!! 【参考方案1】:在您的 RankingResponse 类中替换以下内容:
@SerializedName("data")
@Expose
private List<Datum> data = null;
与:
@SerializedName("data")
private List<Datum> data = new ArrayList<Datum>();
【讨论】:
以上是关于改造无法正确解析正文的主要内容,如果未能解决你的问题,请参考以下文章
如何正确地将数据解析到我的改造客户端中以执行发布请求[重复]
无法使用改造解析对象内部 jsonarray 的 jsonarray?