Android WebView设置Cookie

2022-06-10 12:35:36 浏览数 (1)

业务原理啥的就不讲了,直接上代码
代码语言:javascript复制
    /**
    * 同步cookie
    * @param url 要加载的地址链接
    */
   private void syncCookie(String url) {

       CookieSyncManager.createInstance(this);
       CookieManager cookieManager = CookieManager.getInstance();
       cookieManager.setAcceptCookie(true);
       cookieManager.removeAllCookie();

       //设置cookie
       cookieManager.setCookie(url, SpUtil.getString(GlobalConstant.JESSION_ID)); 

       //获取Cookie
       String mCookie  = cookieManager.getCookie(url);
       //打印查看
       LogUtil.i(mCookie);

       //sync
       CookieSyncManager.getInstance().sync();
   }

这个方法要在mWebview.loadUrl之前调用

0 人点赞