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}}