今天,简单讲讲Android如何使用SharedPreferences保存对象。
记得之前写过一遍博客关于android如何使用SharedPreferences保存List<String>类型的链表,但是最近需要做一个需求,保存一个对象或者是List<Object>,那么应该怎么办呢?在网上查找了资料,最终是解决了问题。这里记录一下。
一.使用Gosn将对象转成String后存入SharedPreferences
下面是我封装改账本(Book)的Bean:
package beans;
/**
* Created by Chase on 2017/5/3.
*/
public class BookBean {
public String bookName;
public int bookBgSrc;
public BookBean(){
}
public BookBean(String bookName,int bookBgSrc){
this.bookName = bookName;
this.bookBgSrc =bookBgSrc;
}
public String getBookName(){
return bookName;
}
public int getBookBgSrc(){
return bookBgSrc;
}
public void setBookBgSrc(int bookBgSrc) {
this.bookBgSrc = bookBgSrc;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
}
下面编写两个方法用来存储和读取这个Bean:
public class SpUtils {
private static SharedPreferences sp;
/**
* 4.存储账本bookBean的list
*/
public static void putBookBean(Context ctx, List<BookBean> bookList) {
if (sp == null) {
sp = ctx.getSharedPreferences("config", MODE_PRIVATE);
}
SharedPreferences.Editor editor = sp.edit();
Gson gson = new Gson();
String json = gson.toJson(bookList);
editor.putString(ConstantValue.BOOK_BEAN, json);
editor.commit();
}
}
/**
* 读取账本bookBean的list
*/
public static List<BookBean> getBookBean(Context ctx) {
if (sp == null) {
sp = ctx.getSharedPreferences("config", MODE_PRIVATE);
}
Gson gson = new Gson();
String json = sp.getString(ConstantValue.BOOK_BEAN, null);
Type type = new TypeToken<List<BookBean>>() {
}.getType();
List<BookBean> arrayList = gson.fromJson(json, type);
return arrayList;
}
这个需要导入Gosn jar包,并且实体类需要满足gosn对于实体类的要求,具体的大家可以查找资料。
二.使用Base64将对象编码成String后存入SharedPreferences
由于二进制数据经过编码后可以用SharedPreferences以字符串的形式存储,所以保存对象也成为了可能,但是这个类必须是可序列化即implements Serializable(实际上Serializable接口是个空接口,只是为了标记该对象是被序列化的),然后可以通过ObjectOutputStream保存再转为二进制存储。
/**
* @param user
*/
public static void saveUser(Context context, String preferenceName,String key,User user) throws Exception {
if(user instanceof Serializable) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceName, context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(user);//把对象写到流里
String temp = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
editor.putString(key, temp);
editor.commit();
} catch (IOException e) {
e.printStackTrace();
}
}else {
throw new Exception("User must implements Serializable");
}
}

2、读取序列化的对象
public static User getUser(Context context, String preferenceName,String key) {
SharedPreferences sharedPreferences=context.getSharedPreferences(preferenceName,context.MODE_PRIVATE);
String temp = sharedPreferences.getString(key, "");
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(temp.getBytes(), Base64.DEFAULT));
User user = null;
try {
ObjectInputStream ois = new ObjectInputStream(bais);
user = (User) ois.readObject();
} catch (IOException e) {
}catch(ClassNotFoundException e1) {
}
return user;
}
当然Sharedpreferences也是可以存储各种集合类的比如说List,都可以通过转为ObjectOutputStream输出流进而编码存储:
public static String listToString(List<?> list)throws IOException {
// 实例化一个ByteArrayOutputStream对象,用来装载压缩后的字节文件。
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 然后将得到的字符数据装载到ObjectOutputStream
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
byteArrayOutputStream);
// writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
objectOutputStream.writeObject(list);
// 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
String listString = new String(Base64.encode(
byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
// 关闭objectOutputStream
objectOutputStream.close();
return listString;
}
@SuppressWarnings("unchecked")
public static List<?> StringToList(String listString) throws StreamCorruptedException, IOException,
ClassNotFoundException {
byte[] mobileBytes = Base64.decode(listString.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
mobileBytes);
ObjectInputStream objectInputStream = new ObjectInputStream(
byteArrayInputStream);
List<?> WeatherList = (List<?>) objectInputStream
.readObject();
objectInputStream.close();
return WeatherList;
}
这个我自己测试过,是可以正常使用的,不过实体类需要实现Serializable接口,而且效率可能比较低,这个大家可以去网上查找资料。
三、关于Base64
public static void main(String[] args) {
BASE64Encoder encoder = new BASE64Encoder();
String s = "Man";
String encoded = encoder.encode(s.getBytes());
System.out.println("ecoded Man " + encoded);
s = "Mo";
String encoded = encoder.encode(s.getBytes());
System.out.println("ecoded Mo" + encoded);
s = "c";
String encoded = encoder.encode(s.getBytes());
System.out.println("ecoded c" + encoded);
}
Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2的6次方等于64,所以每6个比特为一个单元,对应某个可打印字符。三个字节有24个比特,对应于4个Base64单元,即3个字节可表示4个可打印字符。它可用来作为电子邮件的传输编码。在Base64中的可打印字符包括字母A-Z、a-z、0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。Base64是一种可逆的编码方式。最常见的表现就是在于可以用Base64对图片编码变成流,反过来也可以把流转为图片,Base64常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据。包括MIME的email、在XML中存储复杂数据。
这是编码后的数据是一个字符串,其中包含的字符为:A-Z、a-z、0-9、+、/共64个字符(其实是65个字符,而“=”是填充字符)。

当长度为3个字节的数据经过Base64编码后就变为4个字节,比如
如果要编码的字节数不能被3整除,最后会多出1个或2个字节,那么可以使用下面的方法进行处理:先使用0字节值在末尾补足,使其能够被3整除,然后再进行base64的编码。在编码后的base64文本后加上一个或两个’=’号,代表补足的字节数。也就是说,当最后剩余一个八位字节(一个byte)时,最后一个6位的base64字节块有四位是0值,最后附加上两个等号;如果最后剩余两个八位字节(2个byte)时,最后一个6位的base字节块有两位是0值,最后附加一个等号。 参考下表:

android 使用SharedPreferences保存对象就讲完了。
就这么简单。

本文介绍两种在Android中使用SharedPreferences保存对象的方法:使用Gson将对象转为字符串存储及使用Base64编码存储序列化对象。
256

被折叠的 条评论
为什么被折叠?



