dongluni0568
2011-11-28 01:17
浏览 69

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

   }
}

图片转代码服务由CSDN问答提供 功能建议

目前我正在尝试将Cookie存储在我的Android应用中。 我的应用程序正在使用android webview加载网页。 活动如下。

但是,我需要帮助才能在我的应用中存储Cookie。 我正在加载的网页是使用 setcookie()函数用php创建cookie。 它在常规浏览器中工作正常,但我是一个初学者应用程序开发人员,它在我的android WebView中不起作用。

我需要你的帮助来存储cookie与php(在 加载的网页)。

P.S。 我希望cookie永远持续下去(如果可能的话)。

  package com.stuff; 
 
import android.app.Activity; 
import android.os.Bundle; \  nimport android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 
 
public class Activity extends Activity {
  /** 在第一次创建活动时调用。  * / 
 @Override 
 public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState); 
 
 
 //让我们在活动标题栏中显示进度,例如
 //浏览器 应用程序确实。
 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){
 // //活动和WebViews衡量不同比例的进度。  
 //当达到100%时,进度表将自动消失
 activity.setProgress(progress * 1000); 
} 
}); 
 
webview.setWebViewClient(new WebViewClient(){
 
 \ public  void onReceivedError(WebView视图,int errorCode,String description,String failingUrl){
  //如果出现错误,将通知用户(即 没有互联网连接)
 Toast.makeText(活动,“哦不!”+说明,Toast.LENGTH_SHORT).show(); 
} 
}); 
 //这将加载我们想要的网页 见
 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();
    
    点赞 评论

相关推荐 更多相似问题