
在App中嵌入网页,使用Nativie方式登录,然后将cookie保判塌羡存到WebView中,实现免登录功能。同步Cookie到WebView的方法可以参考下面的代码:/*** 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"))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())}}使用上面的方法可以将Cookie同步到WebView中,这样浏览网页时即可实现免登录。但是在实际使用过程中会出现Cookie并未保存成功,每次衫竖都会跳转到登录页面应为初始化WebView时漏掉了重要的东西掘拍。可以参考下面代码设置WebView。/*** 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必须设置支持JavascriptmyWebView.getSettings().setJavaScriptEnabled(true)myWebView.getSettings().setUserAgentString(MyApplication.getUserAgent())myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE)myWebView.getSettings().setAllowFileAccess(true)myWebView.getSettings().setAppCacheEnabled(true)myWebView.getSettings().setDomStorageEnabled(true)myWebView.getSettings().setDatabaseEnabled(true)}完成以上两步 *** 作,再次运行程序,
就会发现,打开网页后不会再跳转到登录页面了。你用IPHONE上的浏览器看网启渣做页记录COOKIE登陆了吗?如果可以,那么你的代码完全不用去考虑处理,系统会自动梁陵保存的。写个网页里写个COOKIE,再次打开APP看看是否能读到就测试悄衡出来了。 到DEVDIV.COM网站查看回答详情>>这个正橡要根据不同的举悔旁网站来定的。如果网站设计者不给Cookies设定
时间(即生命周期),那么就是当次浏览有效,关闭浏览器后前举Cookies就会自动失效(要注意的是手机上的浏览器退出后很可能是切换到后台,并不是真正关闭,这种情况下Cookies不会失效)。如果设定了时间,那么就在时间到后即失效,这个时间可以是几秒钟,也可以是几十年、几百年(相当于永久有效)。
评论列表(0条)