我需要存储我最后一次在客户端Android应用程序上本地触发的查询结果,以便立即使用数据。 稍后可以从AsyncTack或线程
获取更新的数据生成的客户端库返回的对象是扩展GenericJson
的类有没有办法将它转换为String并存储在sharedpreferences中,然后再次实例化Objects?
继承人的课程定义
// 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 */ }
}
答案 0 :(得分:0)
我通过实施一个了不起的库来解决这个问题
使用上述库的示例代码:
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;
}
}
并检索,其简单如下:
cache.get("OBJECT_KEY");