问题描述
通过我下面的code,我已经能够保存一个cookie,但是当我合上的cookie消失的应用程序。
这是怎么造成的,我该怎么解决呢?
包com.jkjljkj
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Window;
进口android.webkit.CookieSyncManager;
进口android.webkit.WebChromeClient;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
进口android.widget.Toast;
公共类活动延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
CookieSyncManager.createInstance(getBaseContext());
//让我们显示在活动标题栏中的进步,像
//浏览器的应用程序一样。
。getWindow()requestFeature(Window.FEATURE_PROGRESS);
的WebView的WebView =新的WebView(本);
的setContentView(web视图);
webview.getSettings()setJavaScriptEnabled(真)。
最后活动活动=这一点;
webview.setWebChromeClient(新WebChromeClient(){
公共无效onProgressChanged(web视图来看,INT进度){
//活动和不同规模WebViews措施的进展。
//当我们达到100%,进度条会自动消失
activity.setProgress(进度* 1000);
}
});
webview.setWebViewClient(新WebViewClient(){
公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
//用户将的情况下被通知有错误(即没有互联网连接)
Toast.makeText(活动,哦,不!+描述,Toast.LENGTH_SHORT).show();
}
});
。CookieSyncManager.getInstance()startSync();
CookieSyncManager.getInstance()同步()。
//这将加载,我们希望看到的网页
webview.loadUrl(HTTP://);
}
}
您必须告诉CookieSyncManager后的同步的已加载的页面有问题。在您的样品code时,的onCreate
方法执行之前完全的的WebView
尝试加载网页,所以同步过程(这恰好异步)可能会完成之前的页面加载。
相反,告诉CookieSyncManager到同步在WebViewClient onPageFinished。你想要什么,应该得到你。
的CookieSyncManager文档是一个良好的阅读对于如何正确地做到这一点。
这里是你可以设置你的WebViewClient实现来为你做这个:
webview.setWebViewClient(新WebViewClient(){
公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
//用户将的情况下被通知有错误(即没有互联网连接)
Toast.makeText(活动,哦,不!+描述,Toast.LENGTH_SHORT).show();
}
公共无效onPageFinished(web视图查看,字符串URL){
CookieSyncManager.getInstance()同步()。
}
);
您不需要告诉CookieSyncManager与此同步,其他地方的地方。我没有测试过这一点,所以让我知道,如果它的工作原理。
With my code below, I have been able to save a cookie, but as soon as I close the application the cookie disappears.
How is this caused and how can I solve it?
package com.jkjljkj
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CookieSyncManager.createInstance(getBaseContext());
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Users will be notified in case there's an error (i.e. no internet connection)
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
CookieSyncManager.getInstance().startSync();
CookieSyncManager.getInstance().sync();
//This will load the webpage that we want to see
webview.loadUrl("http://");
}
}
You have to tell the CookieSyncManager to sync after it has loaded the page in question. In your sample code, the onCreate
method executes completely before the WebView
tries to load the page, so the sync process (which happens asynchronously) will probably complete before the page is loaded.
Instead, tell the CookieSyncManager to sync onPageFinished in the WebViewClient. That should get you what you want.
The CookieSyncManager Documentation is a good read for how to do this properly.
Here is how you could set up your WebViewClient implementation to do this for you:
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Users will be notified in case there's an error (i.e. no internet connection)
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
}
);
You would not need to tell the CookieSyncManager to sync elsewhere with this in place. I haven't tested this, so let me know if it works.
这篇关于如何保存的Cookie在Android的WebView下去吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!