WebView images are not showing with HTTPS

流过昼夜 提交于 2019-12-20 10:29:23

问题


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


}

回答1:


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)




回答2:


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

}



回答3:


Actually it's a SSL property of webview so to handle this you will have to use following code.

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

And in handler you can run webview image url easily.



来源:https://stackoverflow.com/questions/31509277/webview-images-are-not-showing-with-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!