PersistentCookieStore.java
package com.qlzgzg.nettrade.android.cookie;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import com.qlzgzg.nettrade.android.application.MyApplication;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import okhttp3.Cookie;
import okhttp3.HttpUrl;
public class PersistentCookieStore {
private static final String LOG_TAG = "PersistentCookieStore";
private static final String COOKIE_PREFS = "Cookies_Prefs";
private HashMap<String, ConcurrentHashMap<String, Cookie>> cookies;
private Context mContext;
public PersistentCookieStore(Context context) {
cookies = new HashMap<String, ConcurrentHashMap<String, Cookie>>();
mContext = context;
//将持久化的cookies缓存到内存中 即map cookies
HashMap<String, ConcurrentHashMap<String, Cookie>> cookieHashMap = ((MyApplication)mContext.getApplicationContext()).getCookies();
if ((cookieHashMap != null) && (cookieHashMap.size() != 0)) {
cookies = ((MyApplication)mContext.getApplicationContext()).getCookies();
}
}
protected String getCookieToken(Cookie cookie) {
return cookie.name() + "@" + cookie.domain();
}
public void add(HttpUrl url, Cookie cookie) {
String name = getCookieToken(cookie);
//将cookies缓存到内存中 如果缓存过期 就重置此cookie
if (!cookie.persistent()) {
if (!cookies.containsKey(url.host())) {
cookies.put(url.host(), new ConcurrentHashMap<String, Cookie>());
}
cookies.get(url.host()).put(name, cookie);
} else {
if (cookies.containsKey(url.host())) {
cookies.get(url.host()).remove(name);
}
}
//将cookies持久化到本地
((MyApplication)mContext.getApplicationContext()).setCookies(cookies);
}
public List<Cookie> get(HttpUrl url) {
ArrayList<Cookie> ret = new ArrayList<>();
for (String key:cookies.keySet()) {
if (cookies.containsKey(url.host()))
ret.addAll(cookies.get(url.host()).values());
}
return ret;
}
public boolean removeAll() {
cookies.clear();
((MyApplication)mContext.getApplicationContext()).setCookies(null);
return true;
}
public boolean remove(HttpUrl url, Cookie cookie) {
String name = getCookieToken(cookie);
if (cookies.containsKey(url.host()) && cookies.get(url.host()).containsKey(name)) {
cookies.get(url.host()).remove(name);
return true;
} else {
return false;
}
}
public List<Cookie> getCookies() {
ArrayList<Cookie> ret = new ArrayList<>();
for (String key : cookies.keySet())
ret.addAll(cookies.get(key).values());
return ret;
}
}
调用
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final PersistentCookieStore cookieStore = new PersistentCookieStore(context.getApplicationContext());
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
if (cookies != null && cookies.size() > 0) {
for (Cookie item : cookies) {
cookieStore.add(url, item);
}
}
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies;
}
}).build();