我是 Android 网络视图开发的初学者。我应该怎么做才能保留饼干?
I'm a beginner in Android web view development. What should I do to keep the cookies?
我是 Android 网络视图开发的初学者。我应该怎么做才能保留饼干?
我想使用 Android WebView 实现 php 主页。大部分功能都实现了,就是不知道存储id和pw的cookie怎么办
在哪些方面应该如何实施?请教我。
这是我的 mainactivity.java 代码
package com.example.webviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private String url = "http://www.test.com/user/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClientClass());
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
@Override
public void onBackPressed() {
if (webView.getUrl().equalsIgnoreCase("http://www.test.com/index.php") || webView.getUrl().equalsIgnoreCase("http://www.test.com/user/login.php")) {
new AlertDialog.Builder(this)
.setTitle("exit")
.setMessage("exit?")
.setPositiveButton("finish", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
})
.setNegativeButton("no",null)
.show();
}else if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
private class WebViewClientClass extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
要保留 cookie,您可以使用以下方法:
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
您可以通过这种方式从 Webview 中提取当前 url 中的所有 cookie 作为字符串:
@Override
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, "All the cookies in a string:" + cookies);
}
我是 Android 网络视图开发的初学者。我应该怎么做才能保留饼干? 我想使用 Android WebView 实现 php 主页。大部分功能都实现了,就是不知道存储id和pw的cookie怎么办 在哪些方面应该如何实施?请教我。
这是我的 mainactivity.java 代码
package com.example.webviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private String url = "http://www.test.com/user/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClientClass());
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
@Override
public void onBackPressed() {
if (webView.getUrl().equalsIgnoreCase("http://www.test.com/index.php") || webView.getUrl().equalsIgnoreCase("http://www.test.com/user/login.php")) {
new AlertDialog.Builder(this)
.setTitle("exit")
.setMessage("exit?")
.setPositiveButton("finish", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
})
.setNegativeButton("no",null)
.show();
}else if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
private class WebViewClientClass extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
要保留 cookie,您可以使用以下方法:
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
您可以通过这种方式从 Webview 中提取当前 url 中的所有 cookie 作为字符串:
@Override
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, "All the cookies in a string:" + cookies);
}