我正在下面Android-產生的原因:java.text.ParseException:無法解析日期:
{
"idpatient": 48,
"diabetesType": null,
"language": null,
"customId": "CUS790",
"diabetesOther": null,
"firstName": "Nirodha",
"lastName": "Wije",
"email": "[email protected]",
"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
這個響應的JSON響應。以下是我的代碼。
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
,那麼我得到的dob
的格式與Timestamp
格式不一樣。我該如何解決這個問題?
參考HTTP:// stackoverflow.com/questions/22841306/com-google-gson-jsonsyntaxexception-when-trying-to-parse-date-time-in-json – sasikumar