簡體   English   中英

如何在Android WebView中存儲cookie?

[英]How to store cookies in an Android WebView?

目前,我正在嘗試將Cookie存儲在我的android應用中。 我的應用正在使用android webview加載網頁。 該活動如下。

但是,我需要幫助將Cookie存儲在我的應用程序中。 我正在加載的網頁正在使用setcookie()函數通過php創建cookie。 它在常規瀏覽器中可以正常工作,但是我是新手應用開發人員,因此在我的Android WebView中不起作用。

我需要您的幫助,以使用php存儲Cookie(在已加載的網頁上)。

附言:我希望cookie能夠永遠持續下去(如果可能的話)。

package com.stuff;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
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);


        // 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();
}
});
     //This will load the webpage that we want to see
      webview.loadUrl("http://www.need-cookies.com/");

   }
}

看一下CookieSyncManager類,基本上可以做到這一點:

CookieSyncManager syncManager = CookieSyncManager.createInstance(webView.getContext());
CookieManager cookieManager = CookieManager.getInstance();

cookieManager.setCookie(); // Here your cookie
syncManager.sync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM