【问题标题】:Specific webview does not load in layout in Android特定的 webview 不会在 Android 的布局中加载
【发布时间】:2017-06-20 14:03:17
【问题描述】:

我在一个应用程序中有几个 webviews,其中一个根本没有加载,但是当在特定 webview 中更改为一个简单的 url 时,比如“https://www.google.com/”,它可以正确加载。我要加载的网址是“https://mpi.mashie.eu/public/menu/v%C3%A4ster%C3%A5s+stad+skola/a4ec46b2?country=se”,这是一个午餐菜单,位于应用程序内部的 web 视图中,如代码和屏幕截图所示。

@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 上实时运行的应用程序

带有 Webview 的 Android Studio 布局文件

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

【问题讨论】:

    标签: java android webview


    【解决方案1】:

    您正在尝试加载受 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();
        }
    }
    

    【讨论】:

    • Google 不也是受 SSL 保护的吗?
    • 是的,但与您在有问题的场景中尝试加载的网站不同,谷歌拥有有效且受信任的证书。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多