首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Cookies的封存

2012-08-24 
Cookies的保存public class SerializedCookie implements Serializable {private static final long seria

Cookies的保存

public class SerializedCookie implements Serializable {private static final long serialVersionUID = 5327445113190674523L; //arbitraryprivate String name;private String value;private String domain;public SerializedCookie(Cookie cookie){this.name = cookie.getName();this.value = cookie.getValue();this.domain = cookie.getDomain();}public String getName(){return name;}public String getValue(){return value;}public String getDomain(){return domain;}}
DefaultHttpClient client; //declared here, but get the client however you must. @Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);List<Cookie> cookies =client.getCookies();if (!cookies.isEmpty()){Cookie sessionInfo = cookies.get(0);outState.putSerializable("sessionInfo", new SerializedCookie(sessionInfo));}
DefaultHttpClient client; //declared here, but get the client however you must. @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (client.getCookies().isEmpty()){if (savedInstanceState.containsKey("sessionInfo")){SerializedCookie cookie = (SerializedCookie) savedInstanceState.getSerializable("sessionInfo");BasicClientCookie newCookie = new BasicClientCookie(cookie.getName(),cookie.getValue());newCookie.setDomain(cookie.getDomain());client.addCookie(newCookie);} else {                               //for whatever reason the session information couldn't be obtained,                               //take action here}}

热点排行