0

我需要存儲查詢的結果,我上次在本地客戶端android應用程序上觸發以立即使用數據。 更新的數據可以在以後從AsyncTack或線程中獲取任何方式來緩存谷歌雲端點的查詢結果?

,生成的客戶端庫返回的對象是延伸GenericJson

類的有任何方法將其轉換成在sharedpreferences字符串和存儲以後再次實例化對象?

public class MainListingsUpdate extends AsyncTask<Void, Void, Void> { 

    private CollectionResponseMainEventListing list = new CollectionResponseMainEventListing(); 
    private DualCache<CollectionResponseMainEventListing> cache; 

    @Override 
     protected Void doInBackground(Void... voids) { 
      try { 
       list = mainActivityConnect.getAppUserApi().getMainListings().execute(); 



     SizeOf<CollectionResponseMainEventListing> sizeOf = new SizeOf<CollectionResponseMainEventListing>() { 
       @Override 
       public int sizeOf(CollectionResponseMainEventListing object) { 
        return object.toString().getBytes().length; 
       } 
      }; 

     cache = new DualCacheBuilder<>(Constants.cacheName, 1, CollectionResponseMainEventListing.class) 
        .useReferenceInRam(Integer.MAX_VALUE, sizeOf) 
        .useDefaultSerializerInDisk(Integer.MAX_VALUE, true); 

     cache.put("OBJECT_KEY", list); 

      } catch (IOException e) { 
       queryComplete = false; 
      } 
      return null; 
     } 

    } 

和檢索:

繼承人的類定義

// IntelliJ API Decompiler stub source generated from a class file 
    // Implementation of methods is not available 

package sanket.pinboard.backend.postApi.model; 

public final class Post extends com.google.api.client.json.GenericJson { 
    @com.google.api.client.util.Key 
    @com.google.api.client.json.JsonString 
    private java.lang.Long boardID; 
    @com.google.api.client.util.Key 
    private java.lang.String description; 
    @com.google.api.client.util.Key 
    private java.lang.String filrUrl; 
    @com.google.api.client.util.Key 
    @com.google.api.client.json.JsonString 
    private java.lang.Long id; 
    @com.google.api.client.util.Key 
    private java.lang.String imageUrl; 
    @com.google.api.client.util.Key 
    private java.lang.Boolean isOfficial; 
    @com.google.api.client.util.Key 
    private java.lang.Boolean isPinned; 
    @com.google.api.client.util.Key 
    @com.google.api.client.json.JsonString 
    private java.lang.Long reminder; 
    @com.google.api.client.util.Key 
    @com.google.api.client.json.JsonString 
    private java.lang.Long timeStamp; 
    @com.google.api.client.util.Key 
    private java.lang.String title; 
    @com.google.api.client.util.Key 
    private java.lang.String userID; 

    public Post() { /* compiled code */ } 

    public java.lang.Long getBoardID() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setBoardID(java.lang.Long boardID) { /* compiled code */ } 

    public java.lang.String getDescription() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setDescription(java.lang.String description) { /* compiled code */ } 

    public java.lang.String getFilrUrl() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setFilrUrl(java.lang.String filrUrl) { /* compiled code */ } 

    public java.lang.Long getId() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setId(java.lang.Long id) { /* compiled code */ } 

    public java.lang.String getImageUrl() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setImageUrl(java.lang.String imageUrl) { /* compiled code */ } 

    public java.lang.Boolean getIsOfficial() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setIsOfficial(java.lang.Boolean isOfficial) { /* compiled code */ } 

    public java.lang.Boolean getIsPinned() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setIsPinned(java.lang.Boolean isPinned) { /* compiled code */ } 

    public java.lang.Long getReminder() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setReminder(java.lang.Long reminder) { /* compiled code */ } 

    public java.lang.Long getTimeStamp() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setTimeStamp(java.lang.Long timeStamp) { /* compiled code */ } 

    public java.lang.String getTitle() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setTitle(java.lang.String title) { /* compiled code */ } 

    public java.lang.String getUserID() { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post setUserID(java.lang.String userID) { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post set(java.lang.String fieldName, java.lang.Object value) { /* compiled code */ } 

    public sanket.pinboard.backend.postApi.model.Post clone() { /* compiled code */ } 
} 
+1

http://stackoverflow.com/questions/15281164/approach-to-serialize-cloud-endpoint-model-class-to-android-file-system(2日回答)展示瞭如何序列化和重新加載任何GenericJson來自/從'byte []' - 當然如果你願意,你可以根據這些字符建立/解析字符串。 – 2015-02-01 01:00:56

+0

我已經在這裏回答我的同一個問題:http://stackoverflow.com/questions/33485491/how-to-cache-the-response-in-google-cloud-endpoint/34218778#34218778 – penduDev 2015-12-11 08:20:54

+0

@penduDev,我用了現成的圖書館爲它https://github.com/vincentbrison/android-easy-cache – 2015-12-13 07:08:25

回答

0

我通過使用上述文庫實現驚人的文庫

android-easy-cache

示例代碼解決了這個問題,其簡單程度如下:

cache.get("OBJECT_KEY");