Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView images are not showing with HTTPS

My application opens a web view to show HTML page, which is hosted with HTTPS contains one image(image coming from http). On some devices image is not showing but for all other devices its working fine. I checked with multiple devices like Nexus, Samsung s6/s4, Moto G2 and others. Only on Samsung S4/S6, nexus image is not showing. but for all other devices its working fine. Even i tried with WI-FI, data carrier, and multiple OS versions but no luck.

Please help to solve this.

some observations:-

1) On each device i am getting same warning :- [blocked] The page at 'page url' was loaded over HTTPS, but displayed insecure content from 'image source': this content should also be loaded over HTTPS.

2) same page if i am opening in web browser, working fine on all devices.

My Code

mWebView = (WebView) findViewById(R.id.m_web_view);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mainUrl = bean.getUrl();
    mWebView.loadUrl("javascript:window.location.reload( true )");
    mWebView.loadUrl(mainUrl);
    mWebView.setWebViewClient(new myWebClient());



    private class myWebClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
    //some code
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //some code
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler,
            SslError error) {
        handler.proceed();
    }


}
like image 996
Ajit Sharma Avatar asked Jul 20 '15 05:07

Ajit Sharma


People also ask

How do I display HTML content in WebView?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.


2 Answers

Mixed content using HTTP and HTTPS on WebViews are disabled by default starting Lollipop. Is possible that is not working on devices with Lollipop? If this is the case, you can change the default WebView setting on Lollipop using:

webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

Documentation here: http://developer.android.com/reference/android/webkit/WebSettings.html#setMixedContentMode(int)

like image 53
Pollizzio Avatar answered Oct 12 '22 19:10

Pollizzio


Use the follwoing code. You can open https using the following code, extend the onReveivedSslError method of WebViewClient and proceed if any error occurred Here is an example

    WebView webview= (WebView) findViewById(R.id.my_webview);
    webview.setWebViewClient(new WebViewClient() {
     public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
     handler.proceed() ;
     }

}
like image 32
Gopal Singh Sirvi Avatar answered Oct 12 '22 19:10

Gopal Singh Sirvi