Android WebView 未加载混合内容
Android WebView not loading Mixed Content
我正在尝试使用 WebView 制作应用程序,但网站使用的是 https
,但内容(例如 mp3 文件)使用的是 http
,因此 Android Lollipop不会加载它,因为它是 "Mixed Content"。我尝试使用 onReceivedSslError
handler.proceed();
,但它没有加载任何内容。有办法解决吗?或者我可以让所有加载的网站都使用 http
,这样它就不会显示任何错误吗?
Since Pie (API 29), all non-HTTPS traffic in app is now disabled by default.
如果您的目标是 API 26 级或更高级别,您必须先在清单文件中启用它。添加
android:usesCleartextTraffic="true"
进入<application>
标签。
Since Lollipop (API 21), WebView blocks all mixed content by default.
要更改此行为,如果您的目标是 API 21 级或更高级别,请使用:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
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.
在实践中,这应该允许加载图像、视频、音乐等 - 当恶意第三方 tampered/replaced 时,所有内容成为主要安全威胁的可能性很小。
或者使用 (强烈建议):
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
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.
如果你的minAPI小于21,无法直接调用setMixedContentMode,可以使用反射:
try {
Method m = WebSettings.class.getMethod("setMixedContentMode", int.class);
if ( m == null ) {
Log.e("WebSettings", "Error getting setMixedContentMode method");
}
else {
m.invoke(webView.getSettings(), 2); // 2 = MIXED_CONTENT_COMPATIBILITY_MODE
Log.i("WebSettings", "Successfully set MIXED_CONTENT_COMPATIBILITY_MODE");
}
}
catch (Exception ex) {
Log.e("WebSettings", "Error calling setMixedContentMode: " + ex.getMessage(), ex);
}
在 API >= 21 时有条件地加载它,你不必使用反射。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
我最近从 Crosswalk 迁移到使用本机 WebView。
不得不为这个问题奋斗几个小时。修复是在设置设置之前 运行 clearCache()。
webView.clearCache(false); // <-- DO THIS FIRST
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
在androidpie中除了设置混合内容模式外,还需要在AndroidManifest
.
中设置android:usesCleartextTraffic
属性
在你的 AndroidManifest.xml
中做:
<application
....
android:usesCleartextTraffic="true"
...>
并且在设置网络视图时,执行:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
转到 manifest.xml 并添加以下行。
android:usesCleartextTraffic="true"
并在 webview 的 Java 文件中添加此代码。
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
如果您 运行 遇到这个问题,请确保您已经安装了 Ionic 的 WebView Cordova 插件 (https://github.com/ionic-team/cordova-plugin-ionic-webview)。最简单的方法是检查您的 package.json.
安装后:
打开您的 config.xml 文件
检查您是否有 <preference name="Scheme">
的条目
如果这样做,请检查该值是否为“https”。
如果没有,请添加此行:
<preference name="Scheme" value="https" />
添加这一行:
<preference name="MixedContentMode" value="0" />
这解决了我的问题。
我正在尝试使用 WebView 制作应用程序,但网站使用的是 https
,但内容(例如 mp3 文件)使用的是 http
,因此 Android Lollipop不会加载它,因为它是 "Mixed Content"。我尝试使用 onReceivedSslError
handler.proceed();
,但它没有加载任何内容。有办法解决吗?或者我可以让所有加载的网站都使用 http
,这样它就不会显示任何错误吗?
Since Pie (API 29), all non-HTTPS traffic in app is now disabled by default.
如果您的目标是 API 26 级或更高级别,您必须先在清单文件中启用它。添加
android:usesCleartextTraffic="true"
进入<application>
标签。
Since Lollipop (API 21), WebView blocks all mixed content by default.
要更改此行为,如果您的目标是 API 21 级或更高级别,请使用:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
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.
在实践中,这应该允许加载图像、视频、音乐等 - 当恶意第三方 tampered/replaced 时,所有内容成为主要安全威胁的可能性很小。
或者使用 (强烈建议):
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
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.
如果你的minAPI小于21,无法直接调用setMixedContentMode,可以使用反射:
try {
Method m = WebSettings.class.getMethod("setMixedContentMode", int.class);
if ( m == null ) {
Log.e("WebSettings", "Error getting setMixedContentMode method");
}
else {
m.invoke(webView.getSettings(), 2); // 2 = MIXED_CONTENT_COMPATIBILITY_MODE
Log.i("WebSettings", "Successfully set MIXED_CONTENT_COMPATIBILITY_MODE");
}
}
catch (Exception ex) {
Log.e("WebSettings", "Error calling setMixedContentMode: " + ex.getMessage(), ex);
}
在 API >= 21 时有条件地加载它,你不必使用反射。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
我最近从 Crosswalk 迁移到使用本机 WebView。
不得不为这个问题奋斗几个小时。修复是在设置设置之前 运行 clearCache()。
webView.clearCache(false); // <-- DO THIS FIRST
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
在androidpie中除了设置混合内容模式外,还需要在AndroidManifest
.
android:usesCleartextTraffic
属性
在你的 AndroidManifest.xml
中做:
<application
....
android:usesCleartextTraffic="true"
...>
并且在设置网络视图时,执行:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
转到 manifest.xml 并添加以下行。
android:usesCleartextTraffic="true"
并在 webview 的 Java 文件中添加此代码。
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
如果您 运行 遇到这个问题,请确保您已经安装了 Ionic 的 WebView Cordova 插件 (https://github.com/ionic-team/cordova-plugin-ionic-webview)。最简单的方法是检查您的 package.json.
安装后:
打开您的 config.xml 文件
检查您是否有
的条目<preference name="Scheme">
如果这样做,请检查该值是否为“https”。
如果没有,请添加此行:
<preference name="Scheme" value="https" />
添加这一行:
<preference name="MixedContentMode" value="0" />
这解决了我的问题。