存储大量的ArrayList到SQLite的 [英] Storing large ArrayLists to SQLite
问题描述
我即时通讯开发一个应用程序中,我不断地下载大量的数据。
I im developing a application in which I continuously download large amount of data.
JSON格式的数据和我用GSON来反序列化。现在林保存这一个SQLite数据库。我要计算的关系,并将其存储到关系数据库。然而,这需要太多的时间。
The data in json format and I use Gson to deserialize it. By now Im storing this in a SQLite db. And I have to calculate the relationship and store them into a relationship db. However this takes too much time.
这将是更方便的类对象保存到数据库中。
It would be much more convenient to save the class objects to db.
数据看起来像这样反序列化后:
The data looks something like this after deserialization:
Class A{
private String name;
private List<B> subItems;
}
Class B{
private String name;
private List<C> subItems
}
我能坚持这一个更简单的方法,例如通过使它们可序列化的?如何才能做到这一点?
Can I persist this in a easier way, for example by making them serializable? How can this be done?
推荐答案
是的,系列化是你的情况更好的解决方案,它在Android的工作。下面的例子是从<一个取href=\"http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29\">http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29
Yes, serialization is a better solution in your case and it works in Android. The following example is taken from http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29
序列化方法
public static byte[] serializeObject(Object o) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(o);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
return buf;
} catch(IOException ioe) {
Log.e("serializeObject", "error", ioe);
return null;
}
反序列化方法
public static Object deserializeObject(byte[] b) {
try {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));
Object object = in.readObject();
in.close();
return object;
} catch(ClassNotFoundException cnfe) {
Log.e("deserializeObject", "class not found error", cnfe);
return null;
} catch(IOException ioe) {
Log.e("deserializeObject", "io error", ioe);
return null;
}
这篇关于存储大量的ArrayList到SQLite的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!