【发布时间】:2018-05-26 09:07:24
【问题描述】:
我能够解析成功的 200 响应,对于 GoodData 的 pojo 没有问题。但是在 412 上,我想解析响应以获取它返回的数据,以便我可以用它做一些事情。
我有一个 Pojo BadResponseData,但获取它的响应正文始终为空。我尝试过使用转换器,但这需要有一个我似乎无法获得的改造实例才能工作。我错过了什么?我在 Android 文档中找不到任何可以帮助我理解其工作原理的内容。
例如:
public void onResponse(Call<CheckInData> call, Response<GoodData> response) {
if (response.isSuccessful()) {
GoodData goodData = response.body();
if (goodData!= null) {
//do something
}
} else {
BadResponseData badResponseData = response.body();
//badResponseData is always null!! Why?
}
…
返回 412 的 json:
{
“dataToUse”: {
“@name”: “A Name”,
“@date”: "2017-12-11T18:00:00-05:00"
},
"serviceErrors": {
"code": "0",
"fields": “required.data.not.found”,
"message": “name”
}
}
还有我的 pojo 课程:
public class BadResponseData {
private DataToUse dataToUse;
public DataToUse getDataToUse() { return this.dataToUse; }
public void setDataToUse(DataToUse dataToUse) { this.dataToUse = dataToUse; }
private ServiceErrors serviceErrors;
public ServiceErrors getServiceErrors() { return this.serviceErrors; }
public void setServiceErrors(ServiceErrors serviceErrors) { this.serviceErrors = serviceErrors; }
}
public class DataToUse {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private Date date;
public Date getDate() { return this.date; }
public void setDate(Date date) { this.date = date; }
}
public class ServiceErrors {
private String code;
public String getCode() { return this.code; }
public void setCode(String code) { this.code = code; }
private String fields;
public String getFields() { return this.fields; }
public void setFields(String fields) { this.fields = fields; }
private String message;
public String getMessage() { return this.message; }
public void setMessage(String message) { this.message = message; }
}
【问题讨论】:
标签: android error-handling retrofit2 pojo