Storing Large ArrayLists To SQLite
I im developing a application in which I continuously download large amount of data. The data in json format and I use Gson to deserialize it. By now Im storing this in a SQLite db
Solution 1:
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
serialization method:
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;
}
deserialization method:
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;
}
Post a Comment for "Storing Large ArrayLists To SQLite"