【问题标题】:Save Cookies Webview?保存 Cookie 网页视图?
【发布时间】:2011-11-19 20:14:14
【问题描述】:

我正在尝试获取此 WebView for android 代码以保留会话 cookie,以便当应用程序关闭并且用户再次启动它时,他们可以保持登录状态。我对此很陌生,试图弄清楚这一点我感到很沮丧.我需要改变什么?请善待,因为我是个白痴,并且请描述性强,即使您必须为我编辑它,如果这对您来说更容易的话。

我同时导入了 CookieManager 和 CookieSyncManager,但不知道下一步该做什么。我已经阅读了所有文档,并花了几个小时试图弄清楚如何让它保存 cookie,这样用户就不必再次登录,我就是想不通......

package com.template.WebViewTemplate;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

public class WebViewTemplate extends Activity {
private WebView myWebView;
/** Called when the activity is first created. */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { // Enables browsing to previous pages with the hardware back button
        myWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
} 

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myWebView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl("http://zuneboards.com/forums/mgc_chatbox.php?do=view_chatbox"); // Specify the URL to load when the application starts
    //myWebView.loadUrl("file://sdcard/"); // Specify a local file to load when the application starts. Will only load file types WebView supports
    myWebView.setWebViewClient(new WebViewKeep());
    myWebView.setInitialScale(1); // Set the initial zoom scale
    myWebView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component
    myWebView.getSettings().setUseWideViewPort(true); // Initializes double-tap zoom control

}

private class WebViewKeep extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

}}

【问题讨论】:

标签: android eclipse cookies webview


【解决方案1】:

您可以将其与 Web 设置一起使用:

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

同时导入:

import android.webkit.WebSettings;
import android.webkit.WebView;

【讨论】:

    猜你喜欢
    • 2010-09-19
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2019-05-07
    • 2012-03-20
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多