在 Android 网络视图中加载特定的 HTTPS 网络应用程序
Loading specific HTTPS web application in Android webview
我正在尝试加载网络应用程序 https://waveswallet.io,它是 android 网络视图上的区块链钱包应用程序,我的所有尝试都失败了。
我已经为 https 站点尝试了堆栈中提到的所有不同方法,但应用程序拒绝在 webview 上加载,尽管在任何 chrome 或 firefox 浏览器上都能正常工作。
这是我目前使用的代码。
mWebView =(WebView)findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mWebView.loadUrl("https://waveswallet.io");
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
你可以试试这个
try {
if (CommonUtils.isConnectingToInternet(HelpWebviewActivity.this)) {
//check internet connection
WebSettings webSettings = webview_help.getSettings();
webview_help.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
webview_help.getSettings().setSupportZoom(true);
webview_help.getSettings().setBuiltInZoomControls(true);
webview_help.getSettings().setDisplayZoomControls(true);
webview_help.loadUrl(url);
}
else {
CommonUtils.commonToast(HelpWebviewActivity.this, getResources().getString(R.string.no_internet_exist));
}
} catch (Exception e) {
e.printStackTrace();
}
试试这些设置:
WebSettings websettings = mWebView.getSettings();
websettings.setDomStorageEnabled(true);
websettings.setJavaScriptEnabled(true);
使用以下代码 WebViewClient
并处理 onReceivedSslError
方法:
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error){
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
String message = "SSL Certificate error.";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message = "The certificate authority is not trusted.";
break;
case SslError.SSL_EXPIRED:
message = "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message = "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message = "The certificate is not yet valid.";
break;
case SslError.SSL_DATE_INVALID:
message = "The date of the certificate is invalid.";
break;
case SslError.SSL_INVALID:
message = "A generic error occurred.";
break;
case SslError.SSL_MAX_ERROR:
message = "Unknown error occurred.";
break;
}
message += " Do you want to continue anyway?";
builder.setTitle("SSL Certificate Error");
builder.setMessage(message);
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
});
并将此行移动到代码的最后一行并在上面的代码之后:
mWebView.loadUrl("https://waveswallet.io");
UPDATED COMPLETE CODE:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
CookieManager.getInstance().setAcceptCookie(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// If Android 6.0+ i must add support for Third Party Cookies
CookieManager.getInstance().setAcceptThirdPartyCookies(sourceWebView, true);
}
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
//mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setSupportMultipleWindows(false);
mWebView.getSettings().setSupportZoom(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setHorizontalScrollBarEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setUserAgentString("Android WebView");
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(this, "Android");// add this only if required. Vulnerable to mitm attacks.
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());// use as above to handle ssl errors
mWebView.loadUrl("https://waveswallet.io");
输出屏幕截图
Device 1 - Moto E3 Power [Android Marshmallow 6.0, API level 23]
Device 2 - Samsung Galaxy S8 [Android Nougat 7.0, API level 24]
Device 3 - One Plus Five [Android Oreo 8.0.0, API level 26]
希望这能解决您的问题。因为您网站上的 SSL 证书有问题。
我正在尝试加载网络应用程序 https://waveswallet.io,它是 android 网络视图上的区块链钱包应用程序,我的所有尝试都失败了。
我已经为 https 站点尝试了堆栈中提到的所有不同方法,但应用程序拒绝在 webview 上加载,尽管在任何 chrome 或 firefox 浏览器上都能正常工作。
这是我目前使用的代码。
mWebView =(WebView)findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mWebView.loadUrl("https://waveswallet.io");
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
你可以试试这个
try {
if (CommonUtils.isConnectingToInternet(HelpWebviewActivity.this)) {
//check internet connection
WebSettings webSettings = webview_help.getSettings();
webview_help.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
webview_help.getSettings().setSupportZoom(true);
webview_help.getSettings().setBuiltInZoomControls(true);
webview_help.getSettings().setDisplayZoomControls(true);
webview_help.loadUrl(url);
}
else {
CommonUtils.commonToast(HelpWebviewActivity.this, getResources().getString(R.string.no_internet_exist));
}
} catch (Exception e) {
e.printStackTrace();
}
试试这些设置:
WebSettings websettings = mWebView.getSettings();
websettings.setDomStorageEnabled(true);
websettings.setJavaScriptEnabled(true);
使用以下代码 WebViewClient
并处理 onReceivedSslError
方法:
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error){
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
String message = "SSL Certificate error.";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message = "The certificate authority is not trusted.";
break;
case SslError.SSL_EXPIRED:
message = "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message = "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message = "The certificate is not yet valid.";
break;
case SslError.SSL_DATE_INVALID:
message = "The date of the certificate is invalid.";
break;
case SslError.SSL_INVALID:
message = "A generic error occurred.";
break;
case SslError.SSL_MAX_ERROR:
message = "Unknown error occurred.";
break;
}
message += " Do you want to continue anyway?";
builder.setTitle("SSL Certificate Error");
builder.setMessage(message);
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
});
并将此行移动到代码的最后一行并在上面的代码之后:
mWebView.loadUrl("https://waveswallet.io");
UPDATED COMPLETE CODE:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
CookieManager.getInstance().setAcceptCookie(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// If Android 6.0+ i must add support for Third Party Cookies
CookieManager.getInstance().setAcceptThirdPartyCookies(sourceWebView, true);
}
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
//mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setSupportMultipleWindows(false);
mWebView.getSettings().setSupportZoom(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setHorizontalScrollBarEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setUserAgentString("Android WebView");
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(this, "Android");// add this only if required. Vulnerable to mitm attacks.
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());// use as above to handle ssl errors
mWebView.loadUrl("https://waveswallet.io");
输出屏幕截图
Device 1 - Moto E3 Power [Android Marshmallow 6.0, API level 23]
Device 2 - Samsung Galaxy S8 [Android Nougat 7.0, API level 24]
Device 3 - One Plus Five [Android Oreo 8.0.0, API level 26]
希望这能解决您的问题。因为您网站上的 SSL 证书有问题。