0
我正在寻找一个代码示例,演示Storage
interface的实际实际用法。我对HTML5实现特别感兴趣。我刚刚开始研究自己的概念证明,所以如果没有更好的答案,那么我会发布。在PlayN中,如何使用存储接口来保存数据?
存储接口在此谷歌演示文稿这里介绍:
我正在寻找一个代码示例,演示Storage
interface的实际实际用法。我对HTML5实现特别感兴趣。我刚刚开始研究自己的概念证明,所以如果没有更好的答案,那么我会发布。在PlayN中,如何使用存储接口来保存数据?
存储接口在此谷歌演示文稿这里介绍:
下面是一些与PlayN的JSON接口一起演示了如何使用存储接口的代码:
private void loadStoredData() {
// storage parameters
String storageKey = "jsonData";
Json.Object jsonData = PlayN.json().createObject();
// to reset storage, uncomment this line
//PlayN.storage().removeItem(storageKey);
// attempt to load stored data
String jsonString = PlayN.storage().getItem(storageKey);
// if not loaded, create stored data
if (jsonString == null) {
DemoApi.log("stored data not found");
jsonData.put("firstWrite", new Date().toString());
// else display data
} else {
jsonData = PlayN.json().parse(jsonString);
DemoApi.log("stored data loaded");
DemoApi.log("data first written at " + jsonData.getString("firstWrite"));
DemoApi.log("data last read at " + jsonData.getString("lastRead"));
DemoApi.log("data last written at " + jsonData.getString("lastWrite"));
}
// update last read
jsonData.put("lastRead", new Date().toString());
// write data (this works in Java -- not in HTML)
// see https://stackoverflow.com/q/10425877/1093087
/*
Json.Writer jsonWriter = PlayN.json().newWriter();
jsonWriter.object(jsonData).done();
jsonString = jsonWriter.write();
*/
// alternative write routine
Json.Writer jsonWriter = PlayN.json().newWriter();
jsonWriter.object();
for (String key : jsonData.keys()) {
jsonWriter.value(key, jsonData.getString(key));
}
jsonWriter.end();
jsonString = jsonWriter.write();
// store data as json
PlayN.storage().setItem(storageKey, jsonString);
// confirm
if (PlayN.storage().isPersisted()) {
DemoApi.log("data successfully persisted");
} else {
DemoApi.log("failed to persist data");
}
}
有一个与Json.Writer
有点麻烦,似乎有点儿车,我docum在这个问题在这里:In the HTML version of PlayN, why does the following JSON-handling code throw an exception?