android webview 保存 cookie,如何永久保存Android webview中的cookie?

使用下面的代码,我已经能够保存cookie,但是一旦我关闭应用程序,cookie就会消失。

这是怎么造成的,我该如何解决?

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://");

}

}

答案

在加载相关页面后,您必须告诉CookieSyncManager要同步。在示例代码中,onCreate方法在WebView尝试加载页面之前完全执行,因此同步过程(异步发生)可能在页面加载之前完成。

相反,告诉CookieSyncManager在WebViewClient中同步onPageFinished。这应该会得到你想要的。

以下是如何设置WebViewClient实现来为您执行此操作:

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();

}

);

您不需要告诉CookieSyncManager在其他位置同步。我没有测试过这个,所以让我知道它是否有效。

另一答案

.sync()是强制立即同步,并且必须在页面加载后调用它导致它与缓存同步RAM,因此cookie在调用之前必须处于ram状态。

如果您使用此方案,系统每5分钟自动同步一次

onCreate:

CookieSyncManager.createInstance(context)

onResume:

CookieSyncManager.getInstance().startSync()

onPause:

CookieSyncManager.getInstance().stopSync()

我认为你没有等待5分钟,所以系统保存cookie。

另一答案

对于那些面临CookieManager类问题的人,即使在关闭应用程序后仍然使cookie保持不变,他们应该尝试使用flush()的CookieManager函数。

请注意,我没有尝试过这个,所以如果有效的话请告诉我。

根据android文档

确保当前可通过getCookie API访问的所有cookie都写入持久存储。此调用将阻止调用方,直到完成并可能执行I / O.

另外在qazxsw poi文档中写道:

此类在API级别21中已弃用.WebView现在会根据需要自动同步Cookie。您不再需要创建或使用CookieSyncManager。要手动强制同步,您可以使用CookieManager方法flush(),它是sync()的同步替换。 CookieSyncManager

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值