public void putObject(String key , Object obj){
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
String serStr = bos.toString("ISO-8859-1");
serStr = URLEncoder.encode(serStr, "UTF-8");
editer.putString(key, serStr);
editer.commit();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
oos.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Object getObject(String key){
String serStr = preferences.getString(key, "");
ByteArrayInputStream bai = null;
ObjectInputStream ois = null;
Object object = null;
if(serStr != ""){
try {
String sedStr = URLDecoder.decode(serStr, "UTF-8");
bai = new ByteArrayInputStream(sedStr.getBytes("ISO-8859-1"));
ois = new ObjectInputStream(bai);
object = ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ois.close();
bai.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return object;
} To
serialize entities, don't forget to implements Serializable
Original post address:
http://www.eoeandroid.com/thread-902119-1-6.html
[Reproduced] SharedPreferences store java objects, very practical
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326873589&siteId=291194637
Ranking