zoukankan      html  css  js  c++  java
  • Android。WebView加载UR请求使用Cookie储存User_Id记录用户是否登陆过

    1.WebView初始化的时候用倒如下代码:
    if (Build.VERSION.SDK_INT >= 21) {
    CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
    }
    syncCookie(this, url);


    2.实现Cookie储存方法
    /**
    * Sync Cookie
    */
    private void syncCookie(Context context, String url) {
    try {
    CookieSyncManager.createInstance(context);

    cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.removeSessionCookie();// 移除
    cookieManager.removeAllCookie();

    String oldCookie = cookieManager.getCookie(url);
    if (oldCookie != null) {
    Log.d("oldCookie", oldCookie);
    }

    StringBuilder sbCookie = new StringBuilder();
    String userId = BKSetting.getUserId(this);
    sbCookie.append(String.format("user_id=%s", userId));//我自己放的Cookie

    String cookieValue = sbCookie.toString();
    cookieManager.setCookie(url, cookieValue);
    CookieSyncManager.getInstance().sync();


    } catch (Exception e) {
    Log.e("syncCookie failed", e.toString());
    }
    }
  • 相关阅读:
    a.default.ERROR.httpAjax is not a function
    Java heap space
    jmeter
    sql注入
    数据库查前三名
    maven
    国际化变现应用分析
    百度应用部署秘籍
    如何建立起一套有效的APP监控体系
    第三方舆情收集与质量闭环建设
  • 原文地址:https://www.cnblogs.com/lenkevin/p/WebView.html
Copyright © 2011-2022 走看看