Java Android 上的 WebView 和 Cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2566485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebView and Cookies on Android
提问by burak bayramli
I have an application on appspot that works fine through regular browser, however when used through Android WebView, it cannot set and read cookies. I am not trying to get cookies "outside" this web application BTW, once the URL is visited by WebView, all processing, ids, etc. can stay there, all I need is session management inside that application. First screen also loads fine, so I know WebView + server interactivity is not broken.
我在 appspot 上有一个应用程序,它可以通过常规浏览器正常工作,但是当通过 Android WebView 使用时,它无法设置和读取 cookie。顺便说一句,我不是试图在此 Web 应用程序“外部”获取 cookie,一旦 WebView 访问了 URL,所有处理、ID 等都可以保留在那里,我所需要的只是该应用程序内部的会话管理。第一个屏幕也可以正常加载,所以我知道 WebView + 服务器交互性没有中断。
I looked at WebSettings class, there was no call like setEnableCookies.
我查看了 WebSettings 类,没有像 setEnableCookies 这样的调用。
I load url like this:
我像这样加载网址:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl([MY URL]);
}
..
}
Any ideas?
有任何想法吗?
采纳答案by burak bayramli
I figured out what's going on.
我想通了是怎么回事。
When I load a page through a server side action (a url visit), and view the html returned from that action inside a Webview, that first action/page runs inside that Webview. However, when you click on any link that are action commands in your web app, these actions start a new browser. That is why cookie info gets lost because the first cookie information you set for Webview is gone, we have a seperate program here.
当我通过服务器端操作(url 访问)加载页面,并在 Web 视图中查看从该操作返回的 html 时,第一个操作/页面在该 Web 视图中运行。但是,当您单击 Web 应用程序中作为操作命令的任何链接时,这些操作会启动一个新浏览器。这就是 cookie 信息丢失的原因,因为您为 Webview 设置的第一个 cookie 信息消失了,我们这里有一个单独的程序。
You have to intercept clicks on Webview so that browsing never leaves the app, everything stays inside the same Webview.
您必须拦截对 Webview 的点击,以便浏览永远不会离开应用程序,所有内容都保留在同一个 Webview 中。
WebView webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url); //this is controversial - see comments and other answers
return true;
}
});
setContentView(webview);
webview.loadUrl([MY URL]);
This fixes the problem.
这解决了问题。
回答by Pentium10
From the Android documentation:
The
CookieSyncManager
is used to synchronize the browser cookie store between RAM and permanent storage. To get the best performance, browser cookies are saved in RAM. A separate thread saves the cookies between, driven by a timer.To use the
CookieSyncManager
, the host application has to call the following when the application starts:CookieSyncManager.createInstance(context)
To set up for sync, the host application has to call
CookieSyncManager.getInstance().startSync()
in Activity.onResume(), and call
CookieSyncManager.getInstance().stopSync()
in Activity.onPause().
To get instant sync instead of waiting for the timer to trigger, the host can call
CookieSyncManager.getInstance().sync()
The sync interval is 5 minutes, so you will want to force syncs manually anyway, for instance in onPageFinished(WebView, String). Note that even sync() happens asynchronously, so don't do it just as your activity is shutting down.
将
CookieSyncManager
用于同步RAM和永久存储之间的浏览器cookie存储。为了获得最佳性能,浏览器 cookie 保存在 RAM 中。一个单独的线程保存cookies,由计时器驱动。要使用
CookieSyncManager
,宿主应用程序必须在应用程序启动时调用以下代码:CookieSyncManager.createInstance(context)
要设置同步,主机应用程序必须调用
CookieSyncManager.getInstance().startSync()
在 Activity.onResume() 中,并调用
CookieSyncManager.getInstance().stopSync()
在 Activity.onPause() 中。
为了获得即时同步而不是等待定时器触发,主机可以调用
CookieSyncManager.getInstance().sync()
同步间隔为 5 分钟,因此无论如何您都需要手动强制同步,例如在 onPageFinished(WebView, String) 中。请注意,即使 sync() 也是异步发生的,所以不要在您的 Activity 关闭时才这样做。
Finally something like this should work:
最后这样的事情应该工作:
// use cookies to remember a logged in status
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
webview.loadUrl([MY URL]);
回答by Phil
My problem is cookies are not working "within" the same session. –
我的问题是 cookie 在同一个会话“内”不起作用。——
Burak: I had the same problem. Enabling cookies fixed the issue.
布拉克:我也有同样的问题。启用 cookie 解决了这个问题。
CookieManager.getInstance().setAcceptCookie(true);
回答by Miro
CookieManager.getInstance().setAcceptCookie(true);
Normally it should work if your webview is already initialized
CookieManager.getInstance().setAcceptCookie(true);
通常,如果您的 webview 已经初始化,它应该可以工作
or try this:
或者试试这个:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(true);
回答by Huzefa Gadi
If you are using Android Lollipop i.e. SDK 21, then:
如果您使用的是 Android Lollipop ie SDK 21,则:
CookieManager.getInstance().setAcceptCookie(true);
won't work. You need to use:
不会工作。您需要使用:
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
I ran into same issue and the above line worked as a charm.
我遇到了同样的问题,上面的一行很有魅力。