目前我正在尝试在我的android应用程序中存储cookies。我的应用程序正在使用android webview加载网页。活动如下。
但是,我需要帮助在我的应用程序中存储cookies。我正在加载的网页使用setcookie()函数用php创建cookies。它在普通浏览器中运行良好,但我是一个应用程序开发新手,在我的android webview中不起作用。
我需要你的帮助来用php存储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();

09-27 10:01