我正在尝试加载web应用程序https://waveswallet.io这是一个android webview上的区块链钱包应用程序,我的所有尝试都失败了。
我已经尝试了在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());
使用下面的代码到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");
更新的完整代码:
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");
设备1 - Moto E3 Power [Android Marshmallow 6.0,API等级23]
设备2 - 三星Galaxy S8 [Android Nougat 7.0,API级别24]
设备3 - 一加五[Android Oreo 8.0.0,API等级26]
我希望这能解决你的问题。因为您的网站上的SSL证书存在问题。
你可以试试这个
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);