2016-11-17 3 views
2

私が開発したREST APIに接続しているアンドロイドアプリケーションを開発中です。私はJerseyHibernateを使ってREST APIを開発しました。このAPIには、findPatientByIdというメソッドがあります。以下は、そのコード...."JsonParseException:Unparseable date:" - Androidエラー

休憩コール ----

@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; 
    } 
患者ビーン

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; 
     } 

    } 

[OK]を、私はこのRESTメソッドを呼び出すときに、私は以下のJSON出力を取得しています。

{ 
    "idpatient": 86, 
    "diabetesType": null, 
    "language": null, 
    "customId": "CUS790", 
    "diabetesOther": null, 
    "firstName": "Nirodha", 
    "lastName": "Wije", 
    "email": "[email protected]", 
    "dob": "2005-10-16", 
    "parentEmail": "[email protected]", 
    "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 
} 

は今、私はREST APIの作業を処理するためのRetrofitを使用しています、私のアンドロイドアプリケーションからメソッドの上に接続しています。以下は、私はREST呼び出しから取得していますDateTimeStampオブジェクトを変換するために、特定のGSONコンバータを使用しています関連するアンドロイドのコード...

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; 
} 

です。私は上記の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)); 
    } 
} 

このアンドロイドコードを実行すると、以下の例外が発生します。

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: [email protected] time:13147437 

ここで重要なことがあります。彼らは以下の通りです。 Patient豆における

  1. lastUpdateddateCreatedデータベースにTimestampあります。 NetBeansで私はHibernateの自動POJO生成を行い、それらはjava.util.DateとしてBeanで作成されました。 dobは、データベースとBeanの両方でDateオブジェクトです。

  2. 私はMysqlデータベースを使用しています。

上記の例外により、アンドロイドアプリでREST呼び出しを使用してデータベースからBeanを取得することはできません。私はこれが2つの異なったdate形式の残りの呼び出しのため返されていると確信していますが、それは処理する必要があり、そのため私はカスタムGSONコンバータを適用しました。しかし、私は同じ例外を取得しています。私はさまざまな方法で構築されたカスタムGSONコンバータを示唆する数多くのソリューションを検討しましたが、それでも良いものはありませんでした。

どうすればこの問題を解決できますか?あなたはすべての日付形式が法deserialize()に失敗した後、ロングにJSON値を変換し、new Date(valueAsLong)を返すようにしようとすることができ

答えて

0

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)); 
} 

を書くしかし、私は考えます問題はサーバー側にあります。ミリ秒単位で日付を返すべきではありません。

+0

タイムスタンプは、とにかくミリ秒 –

+0

で返されますあなたは私のコードを与えることができますあなたのAndroidコードで提案したことを行う方法についての例? –

+0

@PeakGen私は例を追加 – fRoStBiT

0

あなたの日付フォーマッタに置くために実際の日付を作ってみましょう:

Timestamp stamp = new Timestamp(jsonElement.getAsString()); 
     Date date = new Date(stamp.getTime()); 

その後、このラインでdateを使用します。

return new SimpleDateFormat(format, Locale.US).parse(date); 
+0

どこにこれを置くべきですか? –

+0

私の回答を更新しました –

+0

ありがとうございました。あなたを確認して更新します。 –