2015-12-20 67 views
0

我在java.util.Date類型的Google DataStore中使用new()日期保存Entitiy,然後將其獲取到Android客戶端,並以此格式獲取它:日期序列化Google Cloud Endpoint

yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 

example: '2015-12-27T09:49:51.013Z'

我保存日期在Android客戶端共享偏好在串用這種方法:

.getDate().toStringRfc3339()

後來的後來,當我需要它發送到服務器,我嘗試設置的日期實體以這種方式:

.setDate(stringDateInServerFormatToDate(date)); 

public com.google.api.client.util.DateTime stringDateInServerFormatToDate(String dateInServerFormat){ 
       googleDate = new com.google.api.client.util.DateTime(dateInServerFormat); 

     return googleDate; 
    } 

而且這樣的作品,但是當我試圖把它放在終點在服務器上,我得到這個錯誤:

"message": "com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Can not construct instance of int from String value '2015-12-27T09:49:51.013Z': not a valid Integer value\n at [Source: N/A; line: -1, column: -1] (through reference chain: com.imaginamos.caapturesupervisor.backend.Entities.User[\"userUpdate\"])"

我已經嘗試了許多fromas並沒有成功。 我該如何解決這個問題?

回答

2

也許您正在與之通話的Endpoints API預計時間戳爲簡單整數,如秒或毫秒。這取決於Cloud Endpoints API如何在Bean類中聲明時間戳字段。

如果它使用了com.google.api.server.spi.types.DateAndTime,它應該接受你傳遞的RFC3339字符串。 (同樣可以爲java.util.Date是真實的,但我從來沒有嘗試過。)例如:

import com.google.api.server.spi.types.DateAndTime; 

public class ExampleBean { 
    private DateAndTime timestamp = null; 

    public DateAndTime getTimestamp() { 
    return timestamp; 
    } 
    public void setTimestamp(DateAndTime timestamp) { 
    this.timestamp = timestamp; 
    } 
} 

當一個Bean一樣,建設,其餘JSON發現DOC通告字段作爲時間串,這意味着它應該接受一個RFC3339字符串作爲它的值:

"timestamp": { 
    "type": "string", 
    "format": "date-time" 
} 

DateAndTime是非常愚蠢的,只是字符串的包裝。當它工作時,我立刻把它變成一個喬達日期時間:

import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
... 
DateTime timestamp = new DateTime(
    exampleBean.getTimestamp().toRfc3339String(), 
    DateTimeZone.UTC); 

但是,如果端點API聲明的字段爲intlong,它將不知道如何將您的RFC3339字符串轉換成數,你需要直接發送號碼。

如果您有權訪問Endpoints項目的代碼,請嘗試查看。或者,查看API Explorer是否適用於您,位於https://PROJECT_APP_ID.appspot.com/_ah/api/explorer;它應該可以幫助您找出預期的字段類型。

+0

Hi @dbort我也在開發de Endpoint,你使用com.google.api.server.spi.types.DateAndTime嗎?你可以向我展示這種類型的實例,在服務器和客戶端實現,解析它,thx! –

+0

我在我的答案中添加了更多信息;看看,讓我知道如果它幫助! – dbort

+0

謝謝,我會遲到如果爲我工作,那麼我會接受答案 –

相關問題