关于Android使用Xutils的WebView保存Cookie登录

来源:互联网 发布:mac 下载ipython 编辑:程序博客网 时间:2024/05/21 15:50

参考:

xutils的介绍用法:

http://www.oschina.net/p/xutils

因项目需要,需要在App中嵌入网页,使用Nativie方式登录,然后将cookie保存到WebView中,实现免登录功能。

介绍下XUtils3.0以上的版本保存cookie

DbCookieStore instance = DbCookieStore.INSTANCE;List cookies = instance.getCookies();for (int i = 0; i < cookies.size(); i++) {if ((cookies.get(i)+"").contains("JSESSIONID")) {GBApplication.sessionid = cookies.get(i).toString().replace("JSESSIONID=", "");break;}}


XUtils3.0以下

http://www.mamicode.com/info-detail-861161.html

需要在App中嵌入网页,使用Nativie方式登录,然后将cookie保存到WebView中,实现免登录功能。同步Cookie到WebView的方法网上有大量的参考资料,也可以参考下面的代码:

webview登陆方法

http://my.oschina.net/bingshanguxue/blog/412242

/*** Sync Cookie*/private void syncCookie(Context context, String url){        try{            Log.d("Nat: webView.syncCookie.url", url);                        CookieSyncManager.createInstance(context);             CookieManager cookieManager = CookieManager.getInstance();            cookieManager.setAcceptCookie(true);            cookieManager.removeSessionCookie();// 移除            cookieManager.removeAllCookie();            String oldCookie = cookieManager.getCookie(url);            if(oldCookie != null){                Log.d("Nat: webView.syncCookieOutter.oldCookie", oldCookie);            }             StringBuilder sbCookie = new StringBuilder();            sbCookie.append(String.format("JSESSIONID=%s","INPUT YOUR JSESSIONID STRING"));//上面保存的sessionid            sbCookie.append(String.format(";domain=%s", "INPUT YOUR DOMAIN STRING"));//域名            sbCookie.append(String.format(";path=%s","INPUT YOUR PATH STRING"));//域名下面节点             String cookieValue = sbCookie.toString();            cookieManager.setCookie(url, cookieValue);            CookieSyncManager.getInstance().sync();              String newCookie = cookieManager.getCookie(url);            if(newCookie != null){                Log.d("Nat: webView.syncCookie.newCookie", newCookie);            }        }catch(Exception e){            Log.e("Nat: webView.syncCookie failed", e.toString());        }    }/*** init WebView Settings* */    private void initWebViewSettings(){//        myWebView.getSettings().setSupportZoom(true);//        myWebView.getSettings().setBuiltInZoomControls(true);//        myWebView.getSettings().setDefaultFontSize(12);//        myWebView.getSettings().setLoadWithOverviewMode(true);        // 设置可以访问文件        myWebView.getSettings().setAllowFileAccess(true);        //如果访问的页面中有Javascript,则webview必须设置支持Javascript        myWebView.getSettings().setJavaScriptEnabled(true);        myWebView.getSettings().setUserAgentString("User-Agent:Android");        myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);        myWebView.getSettings().setAllowFileAccess(true);        myWebView.getSettings().setAppCacheEnabled(true);        myWebView.getSettings().setDomStorageEnabled(true);        myWebView.getSettings().setDatabaseEnabled(true);    }


完成以上两步操作,再次运行程序,你会发现,打开网页后不会再跳转到登录页面了。



0 0
原创粉丝点击