Erik
Erik

Reputation: 401

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 Activity with webview running live application

Android Studio layout file with Webview

Android Studio layout file with WebView visible

I have tested an answer to similar question here: Android webview not loading url

Upvotes: 1

Views: 243

Answers (1)

Juginabi
Juginabi

Reputation: 41

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

Upvotes: 1

Related Questions