我需要存储在本地我解雇了最后一次客户端Android应用程序,可以立即使用数据的查询结果。更新后的数据可以稍后从AsyncTack或线程
可读取这是生成的客户端库返回的对象是扩展GenericJson类
有没有办法将它转换成共享preferences一个String和商店,后来又实例化对象?
继承人的类定义
从类文件生成的 //的IntelliJ API闪客存根源 //方法实现不可包sanket.pinboard.backend.postApi.model;公共final类柱延伸com.google.api.client.json.GenericJson { @ com.google.api.client.util.Key @ com.google.api.client.json.JsonString 私人java.lang.Long的板卡ID; @ com.google.api.client.util.Key 私人java.lang.String中描述; @ com.google.api.client.util.Key 私人java.lang.String中filrUrl; @ com.google.api.client.util.Key @ com.google.api.client.json.JsonString 私人java.lang.Long中的ID; @ com.google.api.client.util.Key 私人java.lang.String中图片网址; @ com.google.api.client.util.Key 私人java.lang.Boolean的isOfficial; @ com.google.api.client.util.Key 私人java.lang.Boolean的isPinned; @ com.google.api.client.util.Key @ com.google.api.client.json.JsonString 私人java.lang.Long的提醒; @ com.google.api.client.util.Key @ com.google.api.client.json.JsonString 私人java.lang.Long中的timeStamp; @ com.google.api.client.util.Key 私人java.lang.String中称号; @ com.google.api.client.util.Key 私人java.lang.String中的用户ID; 公共邮政(){/ *编译code * /} 公共java.lang.Long的getBoardID(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setBoardID(java.lang.Long中的板卡ID){/ *编译code * /} 公共java.lang.String中getDescription(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setDescription(java.lang.String中介绍){/ *编译code * /} 公共java.lang.String中getFilrUrl(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setFilrUrl(java.lang.String中filrUrl){/ *编译code * /} 公共java.lang.Long中的getId(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post SETID(java.lang.Long中的id){/ *编译code * /} 公共java.lang.String中getImageUrl(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setImageUrl(java.lang.String中图片网址){/ *编译code * /} 公共java.lang.Boolean的getIsOfficial(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setIsOfficial(java.lang.Boolean的isOfficial){/ *编译code * /} 公共java.lang.Boolean的getIsPinned(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setIsPinned(java.lang.Boolean的isPinned){/ *编译code * /} 公共java.lang.Long的getReminder(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setReminder(java.lang.Long中的提示){/ *编译code * /} 公共java.lang.Long的getTimeStamp(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post的setTimestamp(java.lang.Long中的时间戳){/ *编译code * /} 公共java.lang.String中的getTitle(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post的setTitle(java.lang.String中的标题){/ *编译code * /} 公共java.lang.String中getUserID(){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post setUserID(java.lang.String中的用户ID){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post集(java.lang.String中fieldName的,值的java.lang.Object){/ *编译code * /} 公共sanket.pinboard.backend.postApi.model.Post的clone(){/ *编译code * /}}
解决方案
我通过实现一个惊人的库解决了这个问题。
Android的易缓存
使用上述样本库code:
公共类MainListingsUpdate扩展的AsyncTask<太虚,太虚,太虚> { 私人CollectionResponseMainEventListing名单=新CollectionResponseMainEventListing(); 私人DualCache< CollectionResponseMainEventListing>高速缓存; @覆盖 保护无效doInBackground(虚空......空隙){ 尝试{ 。名单= mainActivityConnect.getAppUserApi()getMainListings()执行(); 一下SizeOf< CollectionResponseMainEventListing>整型尺寸=新中SizeOf< CollectionResponseMainEventListing>(){ @覆盖 公众诠释整型尺寸(CollectionResponseMainEventListing对象){ 返回object.toString()的getBytes()长度。 } }; 缓存=新DualCacheBuilder<>(Constants.cacheName,1,CollectionResponseMainEventListing.class) .useReferenceInRam(Integer.MAX_VALUE的,整型尺寸) .useDefaultSerializerInDisk(Integer.MAX_VALUE的,真正的); cache.put(OBJECT_KEY清单); }赶上(IOException异常五){ queryComplete = FALSE; } 返回null; } }
和检索,它的那样简单:
cache.get(OBJECT_KEY);
I need to store the results of the query that i fired the last time locally on the client android app for immediately using the data. The updated data can be fetched later from an AsyncTack or a thread
The objects that the generated client libraries return are of class that extends GenericJson
is there any way to convert this into a String and store in sharedpreferences and Later instantiate the Objects again ?
heres the class definition
// 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 */ }
}
解决方案
I solved the problem by implementing an amazing library
android-easy-cache
sample code using the above library :
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;
}
}
and to retrieve , its as simple as :
cache.get("OBJECT_KEY");