在混合开发中,有时候需要将原生页面的cookie传到H5的页面,这时就可以用到CookieManager

CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();// 移除
cookieManager.removeAllCookie(); StringBuilder sbCookie = new StringBuilder();
WebUserEntity entity = SPHelp.getInstance().getDao(Constant.SP.webUser, WebUserEntity.class);
sbCookie.append(String.format("UserInfo={\"userId\":\"%s\",\"Account\":\"%s\",\"IntType\":\"%s\",\"appVersion\":\"%s\"}", entity.getUserId(), entity.getAccount(), "" + entity.getLoginType(), App.getAppVsersion()));
sbCookie.append(String.format(";domain=%s", ".xuehu365.com"));
sbCookie.append(String.format(";path=%s", "/")); String cookieValue = sbCookie.toString();
cookieManager.setCookie(url, cookieValue);
CookieSyncManager.getInstance().sync();
05-08 15:31