Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android webview中永久保存cookie?_Java_Android_Cookies_Webview_Cookiestore - Fatal编程技术网

Java 如何在Android webview中永久保存cookie?

Java 如何在Android webview中永久保存cookie?,java,android,cookies,webview,cookiestore,Java,Android,Cookies,Webview,Cookiestore,使用下面的代码,我已经能够保存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 a

使用下面的代码,我已经能够保存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()是强制imediate同步,必须在页面加载后调用,因为它将RAM与缓存同步,所以在调用它之前cookie必须在RAM中

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

onCreate: 
CookieSyncManager.createInstance(context)

onResume:
CookieSyncManager.getInstance().startSync()

onPause:
CookieSyncManager.getInstance().stopSync()

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

对于那些在
CookieManager
类中遇到问题的人,即使在关闭应用程序后,他们也应该尝试
CookieManager
flush()
功能

请注意,我还没有尝试过这个,所以如果它有效的话,请让我也知道

根据android文档

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

CookieSyncManager
文档中还写有:

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


我是全新的。。。你能给我一些实际的代码吗?那会有很大帮助的!好了。试一试。我喜欢程序员们相处的方式:D为所有来到这里并在20年后使用API的人干杯。CookieSyncManager不再需要,已弃用。()cookies现在自动同步。@Daan Pape。正如你提到的,cookies现在自动同步,但当我关闭应用程序并重新打开时,我必须重新登录到webview上的页面!您知道是什么原因导致此问题吗?CookieSyncManager现在已不推荐使用,请使用CookieManager@Rahul Sahni我尝试过,但如果关闭应用程序并重新打开,我必须重新登录,所有cookie都会丢失!你能告诉我可能有什么问题吗?如果我想在android 6上不等待5分钟就立即同步Cookie,我该怎么办?