我有一个服务器,它向我的Android应用程序发送一个会话cookie,用于认证通信.我正在尝试使用指向同一服务器的URL加载WebView,并且我正在尝试传递会话cookie以进行身份验证.我观察到它间歇性地工作,但我不知道为什么.我使用相同的会话cookie在我的服务器上进行其他调用,这些调用永远不会失败.我只是在尝试在WebView中加载URL时才发现此问题,并且每次都不会发生这种情况.很沮丧.
下面是我用来执行此操作的代码.任何帮助将不胜感激.
String myUrl = ""http://mydomain.com/"; CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); Cookie sessionCookie = getCookie(); if(sessionCookie != null){ String cookieString = sessionCookie.getName() +"="+sessionCookie.getValue()+"; domain="+sessionCookie.getDomain(); cookieManager.setCookie(myUrl, cookieString); CookieSyncManager.getInstance().sync(); } WebView webView = (WebView) findViewById(R.id.webview); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new MyWebViewClient()); webView.loadUrl(myUrl);
k7k0.. 51
谢谢justingrammens!这对我有用,我设法在DefaultHttpClient请求和WebView活动中共享cookie:
//------- Native request activity private DefaultHttpClient httpClient; public static Cookie cookie = null; //After Login Listcookies = httpClient.getCookieStore().getCookies(); for (int i = 0; i < cookies.size(); i++) { cookie = cookies.get(i); } //------- Web Browser activity Cookie sessionCookie = myapp.cookie; CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); if (sessionCookie != null) { cookieManager.removeSessionCookie(); String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain(); cookieManager.setCookie(myapp.domain, cookieString); CookieSyncManager.getInstance().sync(); }
小智.. 15
解决方案:Webview CookieSyncManager
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext()); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); cookieManager.removeSessionCookie(); cookieManager.setCookie("http://xx.example.com","mid="+MySession.GetSession().sessionId+" ; Domain=.example.com"); cookieSyncManager.sync(); String cookie = cookieManager.getCookie("http://xx.example.com"); Log.d(LOGTAG, "cookie ------>"+cookie); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setWebViewClient(new TuWebViewClient()); mWebView.loadUrl("http://xx.example.com");
这里的MySession是什么? (4认同)
Jody Jacobus.. 12
感谢Android破坏了我的星期天...下面是修复我的应用程序的内容(在您初始化webview之后)
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) { CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptThirdPartyCookies( webView, true ); }
我应该说上面的答案可能会有效,但在我的情况下,当Android进入v5 +我的android webview javascript'apps'时就已经死了.
谢谢justingrammens!这对我有用,我设法在DefaultHttpClient请求和WebView活动中共享cookie:
//------- Native request activity private DefaultHttpClient httpClient; public static Cookie cookie = null; //After Login Listcookies = httpClient.getCookieStore().getCookies(); for (int i = 0; i < cookies.size(); i++) { cookie = cookies.get(i); } //------- Web Browser activity Cookie sessionCookie = myapp.cookie; CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); if (sessionCookie != null) { cookieManager.removeSessionCookie(); String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain(); cookieManager.setCookie(myapp.domain, cookieString); CookieSyncManager.getInstance().sync(); }
解决方案:Webview CookieSyncManager
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext()); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); cookieManager.removeSessionCookie(); cookieManager.setCookie("http://xx.example.com","mid="+MySession.GetSession().sessionId+" ; Domain=.example.com"); cookieSyncManager.sync(); String cookie = cookieManager.getCookie("http://xx.example.com"); Log.d(LOGTAG, "cookie ------>"+cookie); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setWebViewClient(new TuWebViewClient()); mWebView.loadUrl("http://xx.example.com");
感谢Android破坏了我的星期天...下面是修复我的应用程序的内容(在您初始化webview之后)
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) { CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptThirdPartyCookies( webView, true ); }
我应该说上面的答案可能会有效,但在我的情况下,当Android进入v5 +我的android webview javascript'apps'时就已经死了.
解决方案是给Android足够的时间来处理cookie.您可以在此处找到更多信息:http://code.walletapp.net/post/46414301269/passing-cookie-to-webview