Android WebView js混合cookie和localStorage存储

Android WebView js混合cookie和localStorage存储,第1张

概述一、cookie存储和取出:   (1)存储:   -------------------**在loadURL之前调用**--------------------/***同步一下cookie*/publicvoidsynCookies(Stringurl){CookieManagercookieManager=CookieManager.getInstance()

一、cookie存储和取出:

     (1)存储:     

     -------------------     **在loadURL之前调用**     --------------------    /**     * 同步一下cookie     */    public voID syncookies(String url) {        cookieManager cookieManager = cookieManager.getInstance();        cookieManager.setAcceptcookie(true);        cookieManager.acceptcookie();        cookieManager.removeSessioncookie();// 移除        cookieManager.removeAllcookie();        /**         * cookies是在httpClIEnt中获得的cookie         */        String token = (String) SpUtils.getParam(getApplicationContext(), Constant.TOKEN, "'");        String phone = (String) SpUtils.getParam(getApplicationContext(), Constant.PHONENUMBER, "'");        if (TextUtils.isEmpty(token)) {            return;        }        cookieManager.setcookie(url, Constant.UIcps_USERID + "=" + token);        cookieManager.setcookie(url, Constant.UIcps_USERPHONE + "=" + phone);        /**         *  判断系统当前版本,同步方式不一样         */        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP) {            cookieManager.flush();        } else {            cookieSyncManager.createInstance(getApplicationContext()).sync();        }    }

   

    (2)取出:

       url:web地址
if (cookieManager.getInstance().hascookies()) {//如果存在token就获取 String cookies = cookieManager.getInstance().getcookie(url); }

 

 

二、localstorage存储和取出:  设置localstorage 在onPageFinished中调用

     (1)存储   

         第一步:设置 

 //存储设置
webSettings.setDomStorageEnabled(true); webSettings.setAppCacheMaxSize(1024 * 1024 * 8); String appCachePath = getContext().getCacheDir().getabsolutePath(); webSettings.setAppCachePath(appCachePath);

        第二步:存储

    /**     * 网页加载完毕     */
    @OverrIDe    protected voID onPageFinished(WebVIEw vIEw, String url) {        writelocalstorage();    }    /**     * 写入localstorage     */    private voID writelocalstorage() {        String token = (String) SpUtils.getParam(getApplicationContext(), Constant.TOKEN, "");        String phone = (String) SpUtils.getParam(getApplicationContext(), Constant.PHONENUMBER, "");        if (TextUtils.isEmpty(token)) {            return;        }        if (androID.os.Build.VERSION.SDK_INT >= androID.os.Build.VERSION_CODES.KITKAT) {            if (contentWebVIEw != null) {                contentWebVIEw.evaluateJavaScript("window.localstorage.setItem('" + Constant.UIcps_USERID + "','" + token + "');", null);                contentWebVIEw.evaluateJavaScript("window.localstorage.setItem('" + Constant.UIcps_USERPHONE + "','" + phone + "');", null);            }        } else {            if (contentWebVIEw != null) {                contentWebVIEw.loadUrl("JavaScript:localstorage.setItem('" + Constant.UIcps_USERID + "','" + token + "');");                contentWebVIEw.loadUrl("JavaScript:localstorage.setItem('" + Constant.UIcps_USERPHONE + "','" + phone + "');");            }        }    }

       

      (2)取出

             在前端取出

             

 //token为存入的key值
localstorage.getItem("token")

 

                 

    

 

 

总结

以上是内存溢出为你收集整理的Android WebView js混合cookie和localStorage存储全部内容,希望文章能够帮你解决Android WebView js混合cookie和localStorage存储所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1087707.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存