- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个连接到我开发的 REST API 的 android 应用程序。我使用 Jersey
和 Hibernate
开发了 REST API。在此 API 中,我有一个名为 findPatientById
的方法。下面是它的代码....
休息电话----
@GET
@Path("/findPatientById/{idPatient}")
@Produces(MediaType.APPLICATION_JSON)
public Patient findPatientById(@PathParam("idPatient")int idPatient){
PatientService patientService = new PatientService();
Patient patient = patientService.findPatientById(idPatient);
return patient;
}
数据库层---
public Patient findPatientById(int idPatient, Session session) {
Patient patient = (Patient) session.get(Patient.class, idPatient);
return patient;
}
服务层----
public Patient findPatientById(int idPatient) {
Session session = patientDAOInterface.openCurrentSession();
Transaction transaction = null;
Patient patient = new Patient();
try {
transaction = patientDAOInterface.openTransaction(session);
patient = patientDAOInterface.findPatientById(idPatient, session);
System.out.println("DATE CREATED - "+patient.getDateCreated());
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
session.close();
Language language = new Language();
if (patient.getLanguage() != null) {
Integer idlanguage = patient.getLanguage().getIdlanguage();
language.setIdlanguage(idlanguage);
patient.setLanguage(language);
}
if (patient.getDiabetesType() != null) {
DiabetesType diabetesType = new DiabetesType();
Integer iddiabetesType = patient.getDiabetesType().getIddiabetesType();
diabetesType.setIddiabetesType(iddiabetesType);
patient.setDiabetesType(diabetesType);
}
// patient.setDateCreated(null);
// patient.setLastUpdated(null);
}
return patient;
}
下面是Patient Bean
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
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;
}
}
好的,当我调用此 REST 方法时,我得到以下 JSON
输出。
{
"idpatient": 86,
"diabetesType": null,
"language": null,
"customId": "CUS790",
"diabetesOther": null,
"firstName": "Nirodha",
"lastName": "Wije",
"email": "ni00@gmail.com",
"dob": "2005-10-16",
"parentEmail": "niaasa500@gmail.com",
"gender": "male",
"diagnosedDate": "2016-11-16",
"height": 0,
"weight": 0,
"heightUnit": null,
"weightUnit": null,
"theme": "Lite",
"userName": "ranja",
"password": "N1YL3+M0lnORHnKk0dPN8HYd1IBhWKAGO9Qsop7POgw=",
"dateCreated": 1479311638000,
"lastUpdated": 1479311638000
}
现在,我正在从我的 android 应用程序连接上述方法,我正在使用 Retrofit
来处理 REST API 工作。下面是相关的android代码...
private Patient restcallGetPatient(int idPatient){
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateTypeDeserializer());
Gson gson = gsonBuilder.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RestCommon.URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
PatientEndPoint endPoint = retrofit.create(PatientEndPoint.class);
Call<Patient> call = endPoint.findPatientById(idPatient);
call.enqueue(new Callback<Patient>() {
@Override
public void onResponse(Response<Patient> response, Retrofit retrofit) {
patients=response.body();
Log.d("PROFILE_USER"," PATIENT_DETAILS "+patients.getFirstName());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
Log.d("PROFILE_USER"," PATIENT_ERROR "+t.getMessage());
}
});
return patients;
}
我正在使用特定的 GSON 转换器来转换从 REST 调用中获取的 Date
和 TimeStamp
对象。我在上面的 Retrofit
代码中使用了它。它在下面。
public class DateTypeDeserializer implements JsonDeserializer<Date> {
private String[] DATE_FORMATS = new String[]{
"yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd",
"EEE MMM dd HH:mm:ss z yyyy",
"HH:mm:ss",
"MM/dd/yyyy HH:mm:ss aaa",
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS",
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSS",
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'",
"MMM d',' yyyy H:mm:ss a"
};
@Override
public Date deserialize(JsonElement jsonElement, Type typeOF, JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: \n" + Arrays.toString(DATE_FORMATS));
}
}
现在,当我执行此 android 代码时,出现以下异常。
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: com.google.gson.JsonParseException: Unparseable date: "1479317839000". Supported formats:
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: [yyyy-MM-dd'T'HH:mm:ssZ, yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd, EEE MMM dd HH:mm:ss z yyyy, HH:mm:ss, MM/dd/yyyy HH:mm:ss aaa, yyyy-MM-dd'T'HH:mm:ss.SSSSSS, yyyy-MM-dd'T'HH:mm:ss.SSSSSSS, yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z', MMM d',' yyyy H:mm:ss a]
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at xxx.com.au.xyz.activities.ProfileUserActivity$DateTypeDeserializer.deserialize(ProfileUserActivity.java:474)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at xxx.com.au.xyz.activities.ProfileUserActivity$DateTypeDeserializer.deserialize(ProfileUserActivity.java:451)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at com.google.gson.Gson.fromJson(Gson.java:810)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at com.google.gson.Gson.fromJson(Gson.java:775)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:36)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:24)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at retrofit.OkHttpCall.access$100(OkHttpCall.java:29)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at retrofit.OkHttpCall$1.onResponse(OkHttpCall.java:94)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
11-16 17:37:42.033 6534-6534/xxx.com.au.xyz W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-16 17:37:42.043 6534-6534/xxx.com.au.xyz W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-16 17:37:42.043 6534-6534/xxx.com.au.xyz W/System.err: at java.lang.Thread.run(Thread.java:818)
11-16 17:37:42.043 6534-6534/xxx.com.au.xyz D/PROFILE_USER: PATIENT_ERROR Unparseable date: "1479317839000". Supported formats:
[yyyy-MM-dd'T'HH:mm:ssZ, yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd, EEE MMM dd HH:mm:ss z yyyy, HH:mm:ss, MM/dd/yyyy HH:mm:ss aaa, yyyy-MM-dd'T'HH:mm:ss.SSSSSS, yyyy-MM-dd'T'HH:mm:ss.SSSSSSS, yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z', MMM d',' yyyy H:mm:ss a]
11-16 17:37:42.053 6534-6534/xxx.com.au.xyz I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@b3857a0 time:13147437
好的,现在有非常重要的事情需要注意。它们在下面。
Patient
bean 中的lastUpdated
和dateCreated
是数据库中的Timestamp
。在 NetBeans 中,我为 Hibernate
自动生成 POJO,它们在 bean 中创建为 java.util.Date
。 dob
是数据库和 bean 中的 Date
对象。
我正在使用 Mysql 数据库。
因此,由于上述异常,我不可能在 Android 应用程序中使用 REST 调用从数据库中检索 bean。我很确定这是由于 2 种不同的 date
格式导致 rest 调用返回,但这应该被处理,这就是我应用自定义 GSON
转换器的原因。但没用,我得到了同样的异常(exception)。我研究了许多解决方案,这些解决方案建议以各种方式构建自定义 GSON 转换器,但仍然没有用。
我该如何解决这个问题?
最佳答案
您可以尝试将 JSON 值转换为 Long 并在方法 deserialize()
中所有日期格式失败后返回 new Date(valueAsLong)
:
代替
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: \n" + Arrays.toString(DATE_FORMATS));
写
try {
return new Date(jsonElement.getAsLong());
} catch (ClassCastException e) {
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: \n" + Arrays.toString(DATE_FORMATS));
}
但我认为问题出在服务器端。它不应返回以毫秒为单位的日期。
关于java - "JsonParseException: Unparseable date:"- 安卓错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647335/
我正在尝试使用XMLMapper和ReadTree()来解析一个名为“Message”的字符串变量中的XML文档。我的代码基本上执行以下操作:。有一段时间(真的很长时间),当我运行应用程序时,它没有输
我想做的就是解析非常简单的 json 行,即使它有效我也不知道为什么它会抛出错误 该行是 com.mongodb.util.JSONParseException: {publish_status:'a
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
该消息是在queueChannel尝试发送到特定IP时发出的。 适配器是... @Bean public AbstractConnectionFactory plainClientFactory()
我正在尝试用 Java 处理以下格式的 JSON。 String flowJSON = "{\"FlowDTO\":[{\"policyName\":\"Firewall\",\"action
使用PHP curl方法为大容量数据建立索引时,出现了"error":"JsonParseException[Unexpected character (':' (code 58)): expecte
我正在尝试将 Json 映射到 POJO,但出现此异常: 这是我的代码: ObjectMapper mapper = new ObjectMapper(); try {
尝试反序列化此 JSON 字符串时出现以下异常: { "studentName": "John", "studentAge": "20" } 异常(exception): com.google.gso
我有一个这样的端点: @POST public Response update(MyDocument myDocument){} 如果请求无效,我的服务器会得到一些很长的日志,如下所示: javax.
我正在开发一个连接到我开发的 REST API 的 android 应用程序。我使用 Jersey 和 Hibernate 开发了 REST API。在此 API 中,我有一个名为 findPatie
我正在尝试使用 Java 驱动程序将 json 文件导入 Mongodb,但出现此异常 Exception in thread "main" com.mongodb.util.JSONParseExc
我正在尝试执行以下使用正则表达式的查询 BasicQuery b = new BasicQuery("{ description: /\bmanager\b/}"); 但它抛出 com.mongodb
我正在使用 retrofit(1.9 版)和 jackson(jackson-converter 1.9)。我们的 API 经常以原始字符串的形式发送响应。 我们的 api 客户端如下: RestAd
我正在使用android studio 3.0,无论我做什么,我都无法生成或构建APK,令人惊讶的是它在模拟器上运行,但是在真实的 Android 设备上构建失败。 这就是我尝试构建 APK 时出现错
我正在研究为什么简单的 Jackson JSON 反序列化设置在我明显损坏的 Json 上没有失败。在我的应用程序中,在映射到 Java 类型之前,我必须确认输入是有效的 json。 final St
我已经完成了这样的映射对象到字符串对象的转换 public String getJsonString(Map>> countMap) { Gson gson = new Gson();
我在Android Studio中遇到错误。 我尝试了这些东西,但对我没有用。 删除.gradle文件夹,然后重新同步项目 手动下载了gradle发行版,并且在设置中更改了gradle以从指定位置使用
我的项目在前端使用 Spring MVC,在后端使用 EJB。当我使用 jackson 在 Controller 级别波纹管代码中序列化和反序列化 json 字符串时,代码工作正常但服务层它给出错误“
我的项目在前端使用 Spring MVC,在后端使用 EJB。当我使用 jackson 在 Controller 级别波纹管代码中序列化和反序列化 json 字符串时,代码工作正常但服务层它给出错误“
我有一个 JSON 字符串,我从中创建一个 InputStream 对象,如下所示,然后在尝试序列化我的 JSON 对象时创建一个 GenericRecord 对象Avro 架构。 InputStre
我是一名优秀的程序员,十分优秀!