项目中native和H5的交互免不了需要在webview里面设置Cookie,方法如下:
private void setCookies(String url) {
if(!TextUtils.isEmpty(url)) {
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, cookieManager.getCookie(url));
cookieManager.setCookie(url,"cityid=2");
cookieManager.setCookie(url,"name=swordli");
CookieSyncManager.getInstance().sync();
}
}
如果需要把某一个域名下面统一增加某个Cookie,方法也很简单如下:
cookieManager.setCookie("www.baidu.com","name=swordli");
宝剑锋从磨砺出,梅花香自苦寒来。
网友评论