简体   繁体   English

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

[英]Specific webview does not load in layout in Android

I have several webviews in an application and one of them does not load at all but when changing to a simple url in that specific webview like " https://www.google.com/ " it loads correctly.我在一个应用程序中有几个 webviews,其中一个根本没有加载,但是当在该特定 webview 中更改为一个简单的 url 时,例如“ https://www.google.com/ ”,它可以正确加载。 The url I'm trying to load is " https://mpi.mashie.eu/public/menu/v%C3%A4ster%C3%A5s+stad+skola/a4ec46b2?country=se " which is a lunch menu, in to a webview inside the application like the code and screenshot included shows.我试图加载的网址是“ 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");
}

Application running live on HTC One M9在 HTC One M9 上实时运行的应用程序带有运行实时应用程序的 webview 的活动

Android Studio layout file with Webview带有 Webview 的 Android Studio 布局文件

带有 WebView 可见的 Android Studio 布局文件

I have tested an answer to similar question here: Android webview not loading url我在这里测试了类似问题的答案: Android webview not loading url

You are trying to load SSL secured website (indicated by https://) and you are not handling ssl-error event in your webviewclient.您正在尝试加载 SSL 安全网站(由 https:// 表示)并且您没有在 webviewclient 中处理 ssl-error 事件。 You need to overload onReceivedSslError in webviewclient.您需要在 webviewclient 中重载 onReceivedSslError。 To pass google play store certification you need to create dialog before proceeding with SSL certificate error in your url and let user decide to proceed/cancel ssl error.要通过 Google Play 商店认证,您需要在继续处理 url 中的 SSL 证书错误之前创建对话框,并让用户决定继续/取消 ssl 错误。

This example code is from another post here in StackOverflow which was posted for similar question.此示例代码来自 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM