我在应用程序中有几个webview,其中一个根本没有加载,但在更改为特定webview中的简单URL时,如“https://www.google.com/”,它正确加载。我正在尝试加载的网址是“https://mpi.mashie.eu/public/menu/v%C3%A4ster%C3%A5s+stad+skola/a4ec46b2?country=se”这是一个午餐菜单,位于应用程序内部的webview中,如代码和屏幕截图所示。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lunch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
lunch_view = (WebView)findViewById(R.id.webLunch);
lunch_view.getSettings().setJavaScriptEnabled(true);
lunch_view.setWebViewClient(new WebViewClient());
lunch_view.loadUrl("https://mpi.mashie.eu/public/menu/v%C3%A4ster%C3%A5s+stad+skola/a4ec46b2?country=se");
}
带有Webview的Android Studio布局文件
我在这里测试了类似问题的答案: Android webview not loading url
答案 0 :(得分:1)
您正在尝试加载SSL安全网站(由https://表示),并且您没有在webview客户端中处理ssl-error事件。您需要在webviewclient中重载onReceivedSslError。要通过Google Play商店认证,您需要先创建对话框,然后再在您的网址中继续使用SSL证书错误,并让用户决定继续/取消ssl错误。
此示例代码来自StackOverflow中的另一篇帖子,该帖子已针对类似问题发布。
private class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
AlertDialog.Builder builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext());
AlertDialog alertDialog = builder.create();
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;
}
Log.d(TAG, message);
message += " Do you want to continue anyway?";
alertDialog.setTitle("SSL Certificate Error");
alertDialog.setMessage(message);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Ignore SSL certificate errors
handler.proceed();
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
alertDialog.show();
}
}