class CookieStore_ implements CookieStore{
/** this map may have null keys! */
private final Map<URI, List<HttpCookie>> map = new HashMap<URI, List<HttpCookie>>();
private android.webkit.CookieManager manager;
public CookieStore_() {
manager = android.webkit.CookieManager.getInstance();
}
public synchronized void add(URI uri, HttpCookie cookie) {
if (cookie == null) {
throw new NullPointerException("cookie == null");
}
uri = cookiesUri(uri);
//add cookie to the CookieManager,be sure you have called
//CookieSyncManager.createInstance(context) if your android version
//is lower than Lollipop
manager.setCookie(uri.toString(),cookie.toString());
List<HttpCookie> cookies = map.get(uri);
if (cookies == null) {
cookies = new ArrayList<HttpCookie>();
map.put(uri, cookies);
} else {
cookies.remove(cookie);
}
cookies.add(cookie);
}
private URI cookiesUri(URI uri) {
if (uri == null) {
return null;
}
try {
return new URI("http", uri.getHost(), null, null);
} catch (URISyntaxException e) {
return uri; // probably a URI with no host
}
}
public synchronized List<HttpCookie> get(URI uri) {
if (uri == null) {
throw new NullPointerException("uri == null");
}
List<HttpCookie> result = new ArrayList<HttpCookie>();
// get cookies associated with given URI. If none, returns an empty list
List<HttpCookie> cookiesForUri = map.get(uri);
if (cookiesForUri != null) {
for (Iterator<HttpCookie> i = cookiesForUri.iterator(); i.hasNext(); ) {
HttpCookie cookie = i.next();
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else {
result.add(cookie);
}
}
}
// get all cookies that domain matches the URI
for (Map.Entry<URI, List<HttpCookie>> entry : map.entrySet()) {
if (uri.equals(entry.getKey())) {
continue; // skip the given URI; we've already handled it
}
List<HttpCookie> entryCookies = entry.getValue();
for (Iterator<HttpCookie> i = entryCookies.iterator(); i.hasNext(); ) {
HttpCookie cookie = i.next();
if (!HttpCookie.domainMatches(cookie.getDomain(), uri.getHost())) {
continue;
}
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else if (!result.contains(cookie)) {
result.add(cookie);
}
}
}
return Collections.unmodifiableList(result);
}
public synchronized List<HttpCookie> getCookies() {
List<HttpCookie> result = new ArrayList<HttpCookie>();
for (List<HttpCookie> list : map.values()) {
for (Iterator<HttpCookie> i = list.iterator(); i.hasNext(); ) {
HttpCookie cookie = i.next();
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else if (!result.contains(cookie)) {
result.add(cookie);
}
}
}
return Collections.unmodifiableList(result);
}
public synchronized List<URI> getURIs() {
List<URI> result = new ArrayList<URI>(map.keySet());
result.remove(null); // sigh
return Collections.unmodifiableList(result);
}
public synchronized boolean remove(URI uri, HttpCookie cookie) {
if (cookie == null) {
throw new NullPointerException("cookie == null");
}
List<HttpCookie> cookies = map.get(cookiesUri(uri));
if (cookies != null) {
return cookies.remove(cookie);
} else {
return false;
}
}
public synchronized boolean removeAll() {
boolean result = !map.isEmpty();
map.clear();
return result;
}
public void clearCookies(){
map.clear();
}
}
this class was copied from CookieStoreImpl and added a android.webkit.CookieManager in it,when add(URI uri, HttpCookie cookie) get called, add the cookie to the cookieManager,then when webview load a url which has a match in cookieManager,webview add matched cookies to the request's headers.
below is a helper class
public class CookieUtil {
private CookieManager manager;
private CookieStore_ cookieStore_;
private CookieSyncManager syncManager;
private static CookieUtil cookieUtil;
private boolean isInitialed = false;
private CookieUtil(Context context) {
manager = CookieManager.getInstance();
if (!Util.hasLollipop()){
syncManager = CookieSyncManager.createInstance(context);
}
cookieStore_ = new CookieStore_();
}
public void clearCookies(){
if (manager.hasCookies()){
if (Util.hasLollipop()){
manager.removeAllCookies(null);
}else {
manager.removeAllCookie();
}
}
cookieStore_.clearCookies();
}
public static CookieUtil getCookieUtil(Context context){
if (cookieUtil == null)
cookieUtil = new CookieUtil(context);
return cookieUtil;
}
public void sync(){
if (Util.hasLollipop()){
manager.flush();
}else {
syncManager.sync();
}
}
public void setThirdPartyCookieAcceptable(WebView webView){
if (Util.hasLollipop()){
manager.setAcceptThirdPartyCookies(webView,true);
}
}
public void initCookieHandler(){
if (isInitialed)
return;
isInitialed = true;
CookieHandler.setDefault(new java.net.CookieManager(cookieStore_, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
}
}
if you don't need to save cookies to local storage,you can share volley cookies to webview through a single line
CookieUtil.getCookieUtil(context).initCookieHandler();
and don't forget to call clearCookies() when logout