定义一个工具类:有详细注释
package com.apicloud.A6970406947389.utils; import java.util.Locale; import java.util.Locale; import android.content.Context; import android.content.SharedPreferences; import android.text.TextUtils; import android.util.Log; import com.mechat.loopj.android.http.SerializableCookie; import org.apache.http.client.CookieStore; import org.apache.http.cookie.Cookie; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; public class PersistentCookieStore implements CookieStore { private static final String LOG_TAG = "PersistentCookieStore"; private static final String COOKIE_PREFS = "CookiePrefsFile"; private static final String COOKIE_NAME_STORE = "names"; private static final String COOKIE_NAME_PREFIX = "cookie_"; private boolean omitNonPersistentCookies = false; private final ConcurrentHashMap<String, Cookie> cookies; private final SharedPreferences cookiePrefs; /** * 构建一个持久的cookie store. * * @param context Context to attach cookie store to */ public PersistentCookieStore(Context context) { cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0); cookies = new ConcurrentHashMap<String, Cookie>(); // 加载任何先前储存的cookies到stare String storedCookieNames = cookiePrefs.getString(COOKIE_NAME_STORE, null); if (storedCookieNames != null) { String[] cookieNames = TextUtils.split(storedCookieNames, ","); for (String name : cookieNames) { String encodedCookie = cookiePrefs.getString(COOKIE_NAME_PREFIX + name, null); if (encodedCookie != null) { Cookie decodedCookie = decodeCookie(encodedCookie); if (decodedCookie != null) { cookies.put(name, decodedCookie); } } } // 清除过期的cookie // clearExpired(new Date()); } } //添加cookie时调用 @Override public void addCookie(Cookie cookie) { if (omitNonPersistentCookies && !cookie.isPersistent()) return; String name = cookie.getName() + cookie.getDomain(); // Save cookie into local store, or remove if expired if (!cookie.isExpired(new Date())) { cookies.put(name, cookie); } else { cookies.remove(name); } // Save cookie into persistent store SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet())); prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie))); prefsWriter.commit(); } @Override public void clear() { // Clear cookies from persistent store SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); for (String name : cookies.keySet()) { prefsWriter.remove(COOKIE_NAME_PREFIX + name); } prefsWriter.remove(COOKIE_NAME_STORE); prefsWriter.commit(); // Clear cookies from local store cookies.clear(); } @Override public boolean clearExpired(Date date) { boolean clearedAny = false; SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) { String name = entry.getKey(); Cookie cookie = entry.getValue(); if (cookie.isExpired(date)) { // Clear cookies from local store cookies.remove(name); // Clear cookies from persistent store prefsWriter.remove(COOKIE_NAME_PREFIX + name); // We've cleared at least one clearedAny = true; } } // Update names in persistent store if (clearedAny) { prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet())); } prefsWriter.commit(); return clearedAny; } /** * 强信息保存到本地 * @return */ @Override public List<Cookie> getCookies() { return new ArrayList<Cookie>(cookies.values()); } /** * 将persistentcookiestore实例忽略cookie,这是非持续性的 * signature (`Cookie.isPersistent`) * * @param omitNonPersistentCookies true if non-persistent cookies should be omited */ public void setOmitNonPersistentCookies(boolean omitNonPersistentCookies) { this.omitNonPersistentCookies = omitNonPersistentCookies; } /** * 非标准辅助方法,删除cookie * * @param cookie cookie to be removed */ public void deleteCookie(Cookie cookie) { String name = cookie.getName(); cookies.remove(name); SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); prefsWriter.remove(COOKIE_NAME_PREFIX + name); prefsWriter.commit(); } /** * Cookie对象序列化字符串 * * @param cookie cookie to be encoded, can be null * @return cookie encoded as String */ protected String encodeCookie(SerializableCookie cookie) { if (cookie == null) return null; ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ObjectOutputStream outputStream = new ObjectOutputStream(os); outputStream.writeObject(cookie); } catch (Exception e) { return null; } return byteArrayToHexString(os.toByteArray()); } /** * 从cookie字符串中返回的cookie * * @param cookieString string of cookie as returned from http request * @return decoded cookie or null if exception occured */ protected Cookie decodeCookie(String cookieString) { byte[] bytes = hexStringToByteArray(cookieString); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); Cookie cookie = null; try { ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); cookie = ((SerializableCookie) objectInputStream.readObject()).getCookie(); } catch (Exception exception) { Log.d(LOG_TAG, "decodeCookie failed", exception); } return cookie; } /** * 使用一些超级基本字节数组< - >十六进制转换,所以我们不必依赖于任何 *大Base64图书馆。如果你喜欢的话,可以重写! * * @param bytes byte array to be converted * @return string containing hex values */ protected String byteArrayToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 2); for (byte element : bytes) { int v = element & 0xff; if (v < 16) { sb.append('0'); } sb.append(Integer.toHexString(v)); } return sb.toString().toUpperCase(Locale.US); } /** * 从字符串的字节转换成十六进制值数组 * * @param hexString string of hex-encoded values * @return decoded byte array */ protected byte[] hexStringToByteArray(String hexString) { int len = hexString.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16)); } return data; } }
xutils保存的时候用:
DefaultHttpClient dh = (DefaultHttpClient) http.getHttpClient(); MyCookieStore.cookieStore = dh.getCookieStore(); CookieStore cs = dh.getCookieStore(); List<Cookie> cookies = cs.getCookies(); PersistentCookieStore per = BaseActivity.getInstance().getPersistentCookieStore(); for (Cookie cookie: cookies) { per.addCookie(cookie); }
纯手动保存的时候:
PersistentCookieStore myCookieStore = App.getInstance().getPersistentCookieStore();
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for (Cookie cookie:cookies){
myCookieStore.addCookie(cookie);
}
xtuils在需要的地方调用:
PersistentCookieStore cookieStore = new PersistentCookieStore(BaseActivity.getInstance().getApplicationContext()); PersistentCookieStore cookieStore = new PersistentCookieStore(QianDaoActivity.this); httpUtils.configCookieStore(cookieStore);
纯手动早需要的地方调用:
PersistentCookieStore cookieStore = new PersistentCookieStore(SmthBestApp.getInstance().getApplicationContext());
httpClient.setCookieStore(cookieStore);
HttpResponse response = httpClient.execute(httpget);
有时候会报空指针,将
SmthBestApp.getInstance().getApplicationContext()
改成Activity对象即可。
public static PersistentCookieStore getPersistentCookieStore(){ PersistentCookieStore persistentCookieStore = new PersistentCookieStore(getApplication()); return persistentCookieStore; }