如何永远在Android webview中保存cookie? [英] How to save a cookie in an Android webview forever?

查看:28
本文介绍了如何永远在Android webview中保存cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

   }
}

推荐答案

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

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.

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

Instead, tell the CookieSyncManager to sync onPageFinished in the WebViewClient. That should get you what you want.

CookieSyncManager 文档 是有关如何执行此操作的好读物正确.

The CookieSyncManager Documentation is a good read for how to do this properly.

您可以通过以下方式设置 WebViewClient 实现来为您执行此操作:

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

您不需要告诉 CookieSyncManager 同步到其他地方.我还没有测试过这个,所以如果它有效,请告诉我.

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.

这篇关于如何永远在Android webview中保存cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆