gpt4 book ai didi

android - 保存 Cookies Webview?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:00 33 4
gpt4 key购买 nike

我正在尝试获取此 WebView for android 代码以保留 session 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;
}

}}

最佳答案

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

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

同时导入:

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

关于android - 保存 Cookies Webview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8196956/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com