入门Android开发--笔记--SharedPrefrence存储、读对象
来源:互联网 发布:英语磨耳朵软件 编辑:程序博客网 时间:2024/05/22 12:54
public void savePerson(JSONObject json) throws JSONException{ SharedPreferences preferences = getSharedPreferences("main",Context.MODE_PRIVATE); Person person=new Person(); Iterator it = json.keys();//创建一个遍历器 while (it.hasNext()) { String key = (String) it.next(); String value = json.getString(key);
//对json里的数据逐个读取,设置需要的数据
<span style="white-space:pre"></span> if(key.equals("userid")){ person.setUserid(value); } else if(key.equals("mobile")){ person.setMobile(value); } else if(key.equals("realname")){ person.setRealname(value); else if(key.equals("province")){ person.setProvince(value); } else if(key.equals("city")){ person.setCity(value); } else if(key.equals("county")){ person.setCounty(value); } else if(key.equals("detail")){ person.setDetail(value); } }<span style="white-space:pre"></span>//编码 ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(person);// String productBase64 = new String(org.apache.commons.codec.binary.Base64.encodeBase64(baos .toByteArray()));// SharedPreferences.Editor editor = preferences.edit(); editor.putString("person", productBase64);
editor.apply();//commit立即提交,apply会在后台handle } catch (IOException e) { e.printStackTrace(); } }<pre name="code" class="html"> public Person readPerson() { preferences = getSharedPreferences("main",Context.MODE_PRIVATE); Person person=new Person(); String productBase64 = preferences.getString("person", ""); if (productBase64.equals("")) { return null; } byte[] base64 = Base64.decodeBase64(productBase64.getBytes()); ByteArrayInputStream bais = new ByteArrayInputStream(base64); try { ObjectInputStream bis = new ObjectInputStream(bais); try { person = (Person) bis.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }catch (IOException e) { e.printStackTrace(); } return person; }
0 0
- 入门Android开发--笔记--SharedPrefrence存储、读对象
- SharedPrefrence存储
- Android Sharedprefrence
- sharedPrefrence保存读取对象
- Android学习篇章21-Sharedprefrence
- Android开发入门笔记
- Android开发笔记 -- 文件存储
- 入门Android开发--第一行代码--笔记系列(存储读取Internal文件)
- Android入门笔记 - 数据存储 - SharedPreferences
- Android入门笔记 - 数据存储 - 文件
- Android入门笔记 - 数据存储 - 网络
- Android入门笔记 - 数据存储 - SQLite,单元测试
- android开发之SharedPreferences存储对象
- android开发入门笔记:AIDL简单入门
- 【Android】【笔记】《Android 入门经典》part 10 数据存储
- Android 开发笔记——环境入门
- Android 开发笔记——环境入门
- Android 开发笔记——环境入门
- [bzoj2597][WC2007]剪刀石头布
- 52.View the Exhibit and examine the structure of the PRODUCTS table.
- 物理仿真UIDynamic 介绍 (吸附行为)(推动行为) (一)
- 华为oj 计算日期到天数转换
- 2015年上半年系统分析师试题(附答案)
- 入门Android开发--笔记--SharedPrefrence存储、读对象
- Ruby on Rails 3 Can't connect to local MySQL server through socket '/tmp/mysql.sock' on linux
- 信息更新小红点显示——自定义控件BadgeView的使用介绍
- Pseudo-Random Numbers
- iOS开发-plist文件增删改查
- Activity--生命周期
- 时间和日期迭代
- 微信自定义菜单url默认80端口问题解决
- Linux系统命令权限搜索等练习题