【发布时间】: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