【问题标题】:Serialize an Object and Save in Sqllite DB序列化对象并保存在 Sqlite DB 中
【发布时间】:2014-06-01 02:43:52
【问题描述】:

您好,我在序列化对象时遇到问题。我真正想做的是从服务器获取记录,使用哈希图将其序列化并将其保存在 sqllite db 中。

我可以将序列化的对象保存到我的存储卡中,但是当我反序列化它时,它不会出现,并且我以前的所有数据都被清除了。

这是我的序列化示例代码

序列化

Employee e = new Employee();
  e.name = "Reyan Ali";
  e.address = "Phokka Kuan, Ambehta Peer";
  e.SSN = 11122333;
  e.number = 101;
  try
  {
     FileOutputStream fileOut =
     new FileOutputStream("/tmp/employee.ser");
     ObjectOutputStream out = new ObjectOutputStream(fileOut);
     out.writeObject(e);
     out.close();
     fileOut.close();
     System.out.printf("Serialized data is saved in /tmp/employee.ser");
  }catch(IOException i)
  {
      i.printStackTrace();
  }

}

反序列化

  Employee e = null;
  try
  {
     FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
     ObjectInputStream in = new ObjectInputStream(fileIn);
     e = (Employee) in.readObject();
     in.close();
     fileIn.close();
  }catch(IOException i)
  {
     i.printStackTrace();
     return;
  }catch(ClassNotFoundException c)
  {
     System.out.println("Employee class not found");
     c.printStackTrace();
     return;
  }
  System.out.println("Deserialized Employee...");
  System.out.println("Name: " + e.name);
  System.out.println("Address: " + e.address);
  System.out.println("SSN: " + e.SSN);
  System.out.println("Number: " + e.number);
}

其次,sqlite 中可以保存序列化对象的列类型是什么。 如果你们谁有好的教程,请分享。

【问题讨论】:

  • 你用过google的gson吗?我建议您将对象转换为 json 对象,然后将其存储为数据库作为字符串。

标签: android sqlite serialization android-sqlite


【解决方案1】:

对您的代码进行一些更改,如下所示。

首先在Employee.java类中实现Serializable接口

public void serailize() {
    Employee e = new Employee();
    e.setName("Reyan Ali");
    e.setAddress("Phokka Kuan, Ambehta Peer");
    e.setSSN(11122333);
    e.setNumber(101);
    try {
        ObjectOutputStream out = new ObjectOutputStream(openFileOutput(
                "employee.ser", MODE_PRIVATE));
        out.writeObject(e);
        out.close();
        System.out.printf("Serialized data is saved in /tmp/employee.ser");
    } catch (IOException i) {
        i.printStackTrace();
    }
}

public void deSerailize() {
    Employee e = null;
    try {
        ObjectInputStream in = new ObjectInputStream(
                openFileInput("employee.ser"));
        e = (Employee) in.readObject();
        in.close();
    } catch (IOException i) {
        i.printStackTrace();
        return;
    } catch (ClassNotFoundException c) {
        System.out.println("Serialized class not found");
        c.printStackTrace();
        return;
    }
    System.out.println("Deserialized Employee...");
    System.out.println("Name: " + e.name);
    System.out.println("Address: " + e.address);
    System.out.println("SSN: " + e.SSN);
    System.out.println("Number: " + e.number);
}

OpenFileInput() 和 openFileOutPut() 是应用程序私有文件。

我认为将序列化数据存储在文件中比较好。

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    相关资源
    最近更新 更多