dongluni0568 2011-11-28 01:17
浏览 92
已采纳

如何在Android WebView中存储cookie?

Currently I'm trying to store cookies in my android app. My app is loading a web page using the android webview. The activity is below.

But, I need help to store cookies in my app. The web page I'm loading is creating the cookies with php using the setcookie() function. It works fine in a regular browser, but I'm a beginner app developer and it doesn't work in my android WebView.

I need your help to store the cookie with php (on the loaded web page).

P.S. I want the cookie to last forever (if possible).

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

   }
}
  • 写回答

1条回答 默认 最新

  • douhulao7642 2011-11-28 01:23
    关注

    Take a look at the CookieSyncManager class, basically you can do this:

    CookieSyncManager syncManager = CookieSyncManager.createInstance(webView.getContext());
    CookieManager cookieManager = CookieManager.getInstance();
    
    cookieManager.setCookie(); // Here your cookie
    syncManager.sync();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序