android WebView加载不出网页内容This request has been blocked; the content must be served over HTTPS.

来源:互联网 发布:javascript游戏 编辑:程序博客网 时间:2024/05/16 06:28

 项目中遇到加载网页的时候都出现加载不出内容或者内容不全。但是QQ浏览器或者系统浏览器都没问题,也就是还是自己写的Webview处理有问题。总结了一下大概下面两个问题引起:实际问题可以参看Android Studio 打印的Log,会有Webview的追踪内容为chromium: xxxxxx.


情况一: 

无法加载JS内容写的页面。

设置setDomStorageEnabled,可能是webview前端写的一些代码便签不支持。

WebSettings settings = webview.getSettings();settings.setJavaScriptEnabled(true);settings.setDomStorageEnabled(true);

情况二:

出现部分内容显示部分内容不显示,打印日志大概如下:

chromium: [INFO:CONSOLE(15)] "Mixed Content: The page at 'https://dihao.moxz.cn/' was loaded over HTTPS, but requested an insecure image 'http://sm.domobcdn.com/hm/2017/landing/img/dihao/p5.jpg'. This request has been blocked; the content must be served over HTTPS.", source: https://sm.domobcdn.com/hm/2017/landing/js/swiper.3.4.2.min.js (15)

这个是加载的地址是https的,一些资源文件使用的是http方法的,从安卓4.4之后对webview安全机制有了加强,webview里面加载https url的时候,如果里面需要加载http的资源或者重定向的时候,webview会block页面加载。需要设置MixedContentMode,解决此问题的代码如下:

WebSettings settings = webView.getSettings();                            settings.setJavaScriptEnabled(true);                            settings.setDomStorageEnabled(true);                            settings.setAllowFileAccess(true);                            settings.setAppCacheEnabled(true);                              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {                                webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);                            }  


0 0