Android WebView无法加载混合内容 [英] Android WebView not loading Mixed Content

查看:214
本文介绍了Android WebView无法加载混合内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WebView制作应用程序,但是网站使用的是https,但是内容(例如mp3文件)使用的是http,因此Android Lollipop不会加载它,因为它是混合的"内容".我尝试使用onReceivedSslError handler.proceed();,但是它不会加载任何内容.有办法解决吗?还是可以让所有加载的网站都使用http,这样它不会显示任何错误?

I'm trying to make a app with WebView, but the website is using https, but the content (ex. mp3 file) uses http, so Android Lollipop won't load it because it is "Mixed Content". I tried to use onReceivedSslError handler.proceed();, but it doesn't load anything. Is there a way to fix it? or could I just make all websites loaded use http, so It doesn't show any errors?

推荐答案

从Pie(API 29)开始,现在默认情况下会禁用应用程序中所有非HTTPS流量.

如果您要定位API级别26或更高版本,则必须首先在清单文件中启用它.添加

If you're targeting API level 26 or above, you must first enable it in the manifest file. Add

android:usesCleartextTraffic="true"

插入<application>标签.

由于Lollipop(API 21),WebView会阻止所有混合内容默认情况下.

要更改此行为,当您定位到API级别21或更高版本时,请使用:

To change this behaviour, when you are targeting API level 21 or above, use:

webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);

在这种模式下,WebView将尝试与以下版本兼容: 混合内容方面的现代网络浏览器的一种方法.一些 不安全的内容可能被允许由安全来源加载,并且 其他类型的内容将被阻止.内容的类型是 允许或阻止可能会更改发布版本,而不是 明确定义.

In this mode, the WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content. Some insecure content may be allowed to be loaded by a secure origin and other types of content will be blocked. The types of content are allowed or blocked may change release to release and are not explicitly defined.

在实践中,这应该允许加载图像,视频,音乐等-当被恶意第三方篡改/替换时,所有具有重大安全威胁可能性的内容都是低的.

In practice this should allow loading of images, videos, music etc. - all content that has low probability of being major security threat, when tampered/replaced by malicious third-party.

或者使用(强烈建议):

webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

在此模式下,WebView将允许安全来源加载内容 来自任何其他来源,即使该来源是不安全的.这是 WebView的最低安全操作模式,并在可能的情况下 应用程序不应设置此模式.

In this mode, the WebView will allow a secure origin to load content from any other origin, even if that origin is insecure. This is the least secure mode of operation for the WebView, and where possible apps should not set this mode.

这篇关于Android WebView无法加载混合内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆