将大型 ArrayList 存储到 SQLite

发布于 2024-12-10 21:29:48 字数 365 浏览 0 评论 0原文

我正在开发一个应用程序,在其中不断下载大量数据。

json格式的数据,我使用Gson对其进行反序列化。现在我将其存储在 SQLite 数据库中。我必须计算关系并将它们存储到关系数据库中。然而这需要太多时间。

将类对象保存到数据库会方便得多。

反序列化后的数据看起来像这样:

Class A{
private String name;
private List<B> subItems;
}

Class B{
private String name;
private List<C> subItems
}

我可以用更简单的方式持久化它,例如使它们可序列化吗?这怎么能做到呢?

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2024-12-17 21:29:48

是的,序列化对于您的情况来说是一个更好的解决方案,并且它适用于 Android。以下示例取自 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;
  } 

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;
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文