2016-06-09 1 views
2

REST API를 개발했으며 Android를 사용하여 연결하려고합니다. 아래는 제 코드입니다. Timestamp 변환에 문제가있는 것처럼Android : String 값에서 java.sql.Timestamp 인스턴스를 생성 할 수 없습니다.

private void restCall() 
    { 

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     YourEndpoints request = retrofit.create(YourEndpoints.class); 



     SetupBean setupBean = new SetupBean(); 
      setupBean.setIdPatient(1); 
      setupBean.setCircleType(1); 
      setupBean.setFamiliarity(1); 
      setupBean.setValence(2); 
      setupBean.setArousal(3); 
      setupBean.setDateCreated(Common.getSQLCurrentTimeStamp()); 
      setupBean.setLastUpdated(Common.getSQLCurrentTimeStamp()); 

     Call<ResponseBody> yourResult = request.insertSetup(setupBean); 
     yourResult.enqueue(new Callback<ResponseBody>() { 
      @Override 
      public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 

       try { 
        Log.d("MainActivity", "RESPONSE: " + response.errorBody().string()); 
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 

      } 

      @Override 
      public void onFailure(Call<ResponseBody> call, Throwable t) { 
       try { 
        t.printStackTrace(); 
        Log.d("MainActivity", "RESPONSE: "+"FAILED"); 
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 

     }); 


    } 

내가 이것을 실행하면, 그것은 나에게 아래의 오류

Can not construct instance of java.sql.Timestamp from String value 'Jun 9, 2016 4:24:37 PM': not a valid representation (error: Failed to parse Date value 'Jun 9, 2016 4:24:37 PM': Can not parse date "Jun 9, 2016 4:24:37 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) 

을 보여줍니다 그래서 것 같다. 내 Bean 클래스에서 아래는 Timestamp 정의 방법입니다. 내 restCall() 방법에서

public void setDateCreated(Timestamp dateCreated) { 
     this.dateCreated = dateCreated; 
    } 

    public Timestamp getLastUpdated() { 
     return lastUpdated; 
    } 

, 나는 타임 스탬프를 생성하는 Common.getSQLCurrentTimeStamp()를 호출하고 있습니다. 아래는 그 방법입니다.

public static Timestamp getSQLCurrentTimeStamp() 
    { 
     java.util.Date date = new java.util.Date(); 
     Timestamp t = new Timestamp(date.getTime()); 
     System.out.println(t); 
     return t; 
    } 

나는 restCall() 방법을 실행할 때, 왜 내가이 Can not construct instance of java.sql.Timestamp from String value 'Jun 9, 2016 4:24:37 PM': not a valid representation (error: Failed to parse Date value 'Jun 9, 2016 4:24:37 PM': Can not parse date "Jun 9, 2016 4:24:37 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) 오류는 무엇입니까? 어떻게 해결할 수 있습니까?

+0

이 – Jens

+0

그냥 다른 형식이 날짜를 구문 분석하십시오 전체 스택 트레이스를 추가 날짜 시간 계산기를 제공해야합니다. – Vucko

+0

@Jens : 오류는 없으며'response.errorBody(). string()'을 사용하여 본인이 직접 인쇄하고 있습니다. 나는 이미 'java.lang.String 값에서 java.sql.Timestamp의 인스턴스를 생성 할 수 없습니다'라는 메시지를주었습니다. '2016 년 6 월 9 일 4:16:37 PM': 유효한 표현이 아닙니다. (오류 : 날짜 값을 파싱하지 못했습니다 '2016 년 6 월 9 일 오후 4시 24 분 37 초 ': 날짜를 구문 분석 할 수 없습니다 "2016 년 6 월 9 일 4:24:37 PM": 표준 양식 ("yyyy-MM-dd'T'HH : mm : ss.SSSZ" , "yyyy-MM-dd'HH : mm : ssS'Z '", "EEE, dd MMM yyyy HH : mm : ss zzz", "yyyy-MM-dd")) –

답변

1

나는 이것을 스스로 수정했다. Retrofit 2는 GSON을 사용하고, 그래서 당신은 수동으로

Gson gson = new GsonBuilder() 
       .setDateFormat("yyyy-MM-dd'T'HH:mm:ss") 
       .create(); 

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(URL) 
       .addConverterFactory(GsonConverterFactory.create(gson)) 
       .build(); 
관련 문제