接近序列化云终端模型类到Android文件系统 [英] Approach to serialize cloud endpoint model class to Android file system

查看:146
本文介绍了接近序列化云终端模型类到Android文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地创建一个云终端模式,允许从App Engine的信息便于检索。为了减少往返,并提供更快的用户体验,我已经确定了一个实例,我想保存到本地存储。

I have successfully created a cloud endpoint model that allows for easy retrieval of information from App Engine. To reduce the roundtrips and provide a faster user experience, I have identified one instance I wish to store to local storage.

在我的应用程序的其余部分,我使用ObjectInputStream的读取和写入的对象,如:

Throughout the rest of my app, I am using ObjectInputStream to read and write the objects such as:

FileInputStream fis = context.openFileInput("PRIVFILE");
ObjectInputStream ois = new ObjectInputStream(fis);
AppModelState s = (AppModelState) ois.readObject();

这显然需要的所有数据成员实现Serializable接口。模型类扩展GenericJSON而不是可序列化,如

This obviously requires all data members to implement the Serializable interface. The Model class extends GenericJSON and is not "Serializable", as

    public final class ModelClass extends GenericJson {}

我可以手动创建一个映射到模型中的序列化对象;然而,这似乎很业余,由于属性的数量。

I could manually create a serializable object that maps to the model; however, that seems very amateur due to the number of attributes.

另一种方法我认为是创造,仅仅有JSON字符串为成员,并提供了一​​个二传手/吸气接受ModelClass作为参数,如序列化对象包装:

The other alternative I considered was creating a Serializable Object wrapper that simply has the JSON string as a member and provides a setter/getter accepting the ModelClass as parameters, such as:

class AppModelState implements Serializable {
   private String modelClassJSON;

   public ModelClass getModelClass() {
      // generate a new ModelClass from the JSON
   }

   public void setModelClass(ModelClass c) {
      // extract the JSON for storage
   }

   .....
}

我觉得必须有一个更好的办法,这应该已经解决了十几次,但我没有找到任何资源。请提供意见。

I feel like there must be a better way and this should have been solved a dozen times but I am not finding any resources. Please provide input.

推荐答案

我觉得这样做的,将与端点上使用类标准Java序列化并不能很好地工作。问题是,系列化是二进制的,和HTTP通讯是字符串。

I think that doing standard Java serialization of classes that will be used with Endpoints doesn't work very well. The problem is that serialization is binary, and HTTP comm is string.

如果你是自己做的HTTP通讯,而不是使用终端,我想你有同样的问题。为了送你将序列化对象(一个字符串成员转换为二进制),然后你将不得不二进制转换回字符串。

If you were doing the HTTP comm yourself, rather then using endpoints, I think you would have the same problem. In order to send the object you would serialize it (converting an string members to binary) and then you would have to convert the binary back to string.

所以,如果你正在使用的数据量不是太大,它很可能是最简单的方法你的对象存储为JSON。

So, if the amount of data you are using is not too much, it would probably be easiest to store your objects as JSON.

这篇关于接近序列化云终端模型类到Android文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆