搜索
热搜: 活动 交友 discuz
查看: 6592|回复: 0
打印 上一主题 下一主题

Android WebView保存Cookie登录

[复制链接]

160

主题

165

帖子

814

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
814
跳转到指定楼层
楼主
发表于 2016-1-10 17:32:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
因项目需要,需要在App中嵌入网页,使用Nativie方式登录,然后将cookie保存到WebView中,实现免登录功能。同步Cookie到WebView的方法网上有大量的参考资料,也可以参考下面的代码:


  1. /**
  2. * Sync Cookie
  3. */
  4. private void syncCookie(Context context, String url){
  5.         try{
  6.             Log.d("Nat: webView.syncCookie.url", url);           

  7.             CookieSyncManager.createInstance(context);

  8.             CookieManager cookieManager = CookieManager.getInstance();
  9.             cookieManager.setAcceptCookie(true);
  10.             cookieManager.removeSessionCookie();// 移除
  11.             cookieManager.removeAllCookie();
  12.             String oldCookie = cookieManager.getCookie(url);
  13.             if(oldCookie != null){
  14.                 Log.d("Nat: webView.syncCookieOutter.oldCookie", oldCookie);
  15.             }

  16.             StringBuilder sbCookie = new StringBuilder();
  17.             sbCookie.append(String.format("JSESSIONID=%s","INPUT YOUR JSESSIONID STRING"));
  18.             sbCookie.append(String.format(";domain=%s", "INPUT YOUR DOMAIN STRING"));
  19.             sbCookie.append(String.format(";path=%s","INPUT YOUR PATH STRING"));

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

  23.             String newCookie = cookieManager.getCookie(url);
  24.             if(newCookie != null){
  25.                 Log.d("Nat: webView.syncCookie.newCookie", newCookie);
  26.             }
  27.         }catch(Exception e){
  28.             Log.e("Nat: webView.syncCookie failed", e.toString());
  29.         }
  30.     }
复制代码

使用上面的方法可以将Cookie同步到WebView中,这样浏览网页时即可实现免登录。
但是在实际使用过程中发现Cookie并未保存成功,每次都会跳转到登录页面,纠结了很久,终于发现是在初始化WebView时漏掉了重要的东西。可以参考下面我的代码设置WebView。

  1. /**
  2. * init WebView Settings
  3. * */
  4.     private void initWebViewSettings(){
  5. //        myWebView.getSettings().setSupportZoom(true);
  6. //        myWebView.getSettings().setBuiltInZoomControls(true);
  7. //        myWebView.getSettings().setDefaultFontSize(12);
  8. //        myWebView.getSettings().setLoadWithOverviewMode(true);
  9.         // 设置可以访问文件
  10.         myWebView.getSettings().setAllowFileAccess(true);
  11.         //如果访问的页面中有Javascript,则webview必须设置支持Javascript
  12.         myWebView.getSettings().setJavaScriptEnabled(true);
  13.         myWebView.getSettings().setUserAgentString(MyApplication.getUserAgent());
  14.         myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
  15.         myWebView.getSettings().setAllowFileAccess(true);
  16.         myWebView.getSettings().setAppCacheEnabled(true);
  17.         myWebView.getSettings().setDomStorageEnabled(true);
  18.         myWebView.getSettings().setDatabaseEnabled(true);
  19.     }
复制代码
完成以上两步操作,再次运行程序,你会发现,打开网页后不会再跳转到登录页面了。
第一使用WebView控件,原以为很简单,可是一不小心就掉坑里去了,大家小心。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|安卓论坛  

GMT+8, 2024-5-18 15:59 , Processed in 0.063048 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2013 Design S!|ƽ̶

快速回复 返回顶部 返回列表