java - 特定的 webview 不会在 Android 的布局中加载

标签 java android webview

我在一个应用程序中有多个 web View ,其中一个根本不加载,但是当更改为该特定 web View 中的简单 url(如“https://www.google.com/”)时,它会正确加载。我要加载的 url 是“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");
}

在 HTC One M9 上实时运行的应用程序 Activity with webview running live application

带有 Webview 的 Android Studio 布局文件

Android Studio layout file with WebView visible

我在这里测试了类似问题的答案: Android webview not loading url

最佳答案

您正在尝试加载受 SSL 保护的网站(由 https://表示),并且您没有在 webviewclient 中处理 ssl-error 事件。您需要在 webviewclient 中重载 onReceivedSslError。要通过 google play store 认证,您需要在 url 中出现 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();
    }
}

关于java - 特定的 webview 不会在 Android 的布局中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020194/

相关文章:

java - 在 JUnit 中打印测试结果

java - 是否可以使用 Apache POI XSSF 设置 Activity 范围?

java - Android Studio 上的 setEGLContextClientVersion(3) 导致错误

javascript - WindowsPhone native 应用程序 - 与 Javascript/HTML5/Canvas/CSS 3 的兼容性

iphone - 带有 Facebook Connect 的移动 Web 应用程序的空白屏幕

java - 在每个 jvm 运行中添加 jar 文件似乎不起作用

java - 在 OS X 10.9 Mavericks 上安装 Java 源代码

java - 触摸位置周围的半径

android - 如何避免 `getNotification` 被弃用?

android - ScrollView中的WebView在向上滑动时调用refreshControl React Native Android一直