我得到了下面的JSON响应
{
"idpatient": 48,
"diabetesType": null,
"language": null,
"customId": "CUS790",
"diabetesOther": null,
"firstName": "Nirodha",
"lastName": "Wije",
"email": "ni00@gmail.com",
"dob": "1987-10-11",
"parentEmail": null,
"gender": "male",
"diagnosedDate": "2016-11-11",
"height": 0,
"weight": 0,
"heightUnit": null,
"weightUnit": null,
"theme": "Lite",
"userName": "fhjjjghhh",
"password": "asdf5555",
"dateCreated": 1478871293000,
"lastUpdated": 1478871293000
}
我正在使用Android中的Retrofit
捕捉到这个响应。下面是我的密码。
private void restCallFindPatient(int idPatient, final String email, final String password) {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RestCommon.URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
PatientEndPoint patientEndPoint = retrofit.create(PatientEndPoint.class);
Call<Patient> patientCall = patientEndPoint.findPatientById(idPatient);
patientCall.enqueue(new Callback<Patient>() {
@Override
public void onResponse(Response<Patient> response, Retrofit retrofit) {
Patient patient = response.body();
Log.d("LOGIN_ACTIVITY", " PATIENT " + patient.getFirstName());
} else {
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
Log.d("LOGIN_ACTIVITY", " ERROR_MESSAGE " + t.getLocalizedMessage());
}
});
}
我的PatientBean
如下。
public class Patient implements java.io.Serializable {
private Integer idpatient;
private DiabetesType diabetesType;
private Language language;
private String customId;
private String diabetesOther;
private String firstName;
private String lastName;
private String email;
private Date dob;
private String parentEmail;
private String gender;
private Date diagnosedDate;
private Double height;
private Double weight;
private String heightUnit;
private String weightUnit;
private String theme;
private String userName;
private String password;
private Date dateCreated;
private Date lastUpdated;
public Patient() {
}
public Patient(DiabetesType diabetesType, Language language, String customId, String firstName, String email, Date dob, String gender, String theme, String userName, String password, Date lastUpdated) {
this.diabetesType = diabetesType;
this.language = language;
this.customId = customId;
this.firstName = firstName;
this.email = email;
this.dob = dob;
this.gender = gender;
this.theme = theme;
this.userName = userName;
this.password = password;
this.lastUpdated = lastUpdated;
}
public Patient(DiabetesType diabetesType, Language language, String customId, String diabetesOther, String firstName, String lastName, String email, Date dob, String parentEmail, String gender, Date diagnosedDate, Double height, Double weight, String heightUnit, String weightUnit, String theme, String userName, String password, Date dateCreated, Date lastUpdated) {
this.diabetesType = diabetesType;
this.language = language;
this.customId = customId;
this.diabetesOther = diabetesOther;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.dob = dob;
this.parentEmail = parentEmail;
this.gender = gender;
this.diagnosedDate = diagnosedDate;
this.height = height;
this.weight = weight;
this.heightUnit = heightUnit;
this.weightUnit = weightUnit;
this.theme = theme;
this.userName = userName;
this.password = password;
this.dateCreated = dateCreated;
this.lastUpdated = lastUpdated;
}
public Integer getIdpatient() {
return this.idpatient;
}
public void setIdpatient(Integer idpatient) {
this.idpatient = idpatient;
}
public DiabetesType getDiabetesType() {
return this.diabetesType;
}
public void setDiabetesType(DiabetesType diabetesType) {
this.diabetesType = diabetesType;
}
public Language getLanguage() {
return this.language;
}
public void setLanguage(Language language) {
this.language = language;
}
public String getCustomId() {
return this.customId;
}
public void setCustomId(String customId) {
this.customId = customId;
}
public String getDiabetesOther() {
return this.diabetesOther;
}
public void setDiabetesOther(String diabetesOther) {
this.diabetesOther = diabetesOther;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDob() {
return this.dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getParentEmail() {
return this.parentEmail;
}
public void setParentEmail(String parentEmail) {
this.parentEmail = parentEmail;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getDiagnosedDate() {
return this.diagnosedDate;
}
public void setDiagnosedDate(Date diagnosedDate) {
this.diagnosedDate = diagnosedDate;
}
public Double getHeight() {
return this.height;
}
public void setHeight(Double height) {
this.height = height;
}
public Double getWeight() {
return this.weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public String getHeightUnit() {
return this.heightUnit;
}
public void setHeightUnit(String heightUnit) {
this.heightUnit = heightUnit;
}
public String getWeightUnit() {
return this.weightUnit;
}
public void setWeightUnit(String weightUnit) {
this.weightUnit = weightUnit;
}
public String getTheme() {
return this.theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getLastUpdated() {
return this.lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
}
当我运行我的代码时,我会得到以下错误,
11-11 13:35:31.156 24750-24750/xxx W/System.err: com.google.gson.JsonSyntaxException: 1478871293000
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.Gson.fromJson(Gson.java:810)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.google.gson.Gson.fromJson(Gson.java:775)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:36)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:24)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at retrofit.OkHttpCall.access$100(OkHttpCall.java:29)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at retrofit.OkHttpCall$1.onResponse(OkHttpCall.java:94)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-11 13:35:31.176 24750-24750/xxx W/System.err: at java.lang.Thread.run(Thread.java:841)
11-11 13:35:31.176 24750-24750/xxx W/System.err: Caused by: java.text.ParseException: Unparseable date: "1478871293000" (at offset 13)
11-11 13:35:31.216 24750-24750/xxx D/dalvikvm: GC_FOR_ALLOC freed 727K, 30% free 7326K/10412K, paused 33ms, total 33ms
11-11 13:35:31.216 24750-24750/xxx W/System.err: at java.text.DateFormat.parse(DateFormat.java:555)
11-11 13:35:31.216 24750-24750/xxx W/System.err: at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)
11-11 13:35:31.216 24750-24750/xxx W/System.err: ... 17 more
11-11 13:35:31.216 24750-24750/xxx D/LOGIN_ACTIVITY: ERROR_MESSAGE 1478871293000
请注意,在我的JSON响应中,我既有Timestamp
类型,也有Date
类型。如果我将Gson
转换代码的日期格式更改为yyyy-MM-dd HH:mm:ss
,那么对于不是Timestamp
格式的dob
,也会出现相同的错误。我怎么才能解决这个问题?
发布于 2016-11-11 08:33:23
您的dateCreated
实际上不是Date
。它是包含毫秒数的Long
,从Unix Time开始传递(Epoch,1970-1-1)。您可以为它创建一个自定义反序列化器,但我建议的是将您的字段声明为Long
而不是Date
,并在getter\setter中将其转换为Date
:
private Long dateCreated;
private Long lastUpdated;
public Date getDateCreated() {
return new Date(this.dateCreated);
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated.getTime();
}
public Date getLastUpdated() {
return new Date(this.lastUpdated);
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated.getTime();
}
https://stackoverflow.com/questions/40543816
复制相似问题