简体   繁体   中英

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. 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.

@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带有运行实时应用程序的 webview 的活动

Android Studio layout file with Webview

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

I have tested an answer to similar question here: 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. You need to overload onReceivedSslError in webviewclient. 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.

This example code is from another post here in StackOverflow which was posted for similar question.

    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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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