简体   繁体   English

Android WebView 未加载混合内容

[英]Android WebView not loading Mixed Content

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".我正在尝试使用 WebView 制作一个应用程序,但该网站使用的是https ,但内容(例如 mp3 文件)使用的是http ,因此 Android Lollipop 不会加载它,因为它是“混合内容”。 I tried to use onReceivedSslError handler.proceed();我尝试使用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?或者我可以让所有加载的网站都使用http ,所以它不显示任何错误?

Since Pie (API 29), all non-HTTPS traffic in app is now disabled by default . 自 Pie (API 29) 起,应用程序中的所有非 HTTPS 流量现在默认禁用

If you're targeting API level 26 or above , you must first enable it in the manifest file.如果您的目标是 API 级别 26 或更高级别,则必须首先在清单文件中启用它。 Add添加

android:usesCleartextTraffic="true"

into <application> tag.进入<application>标签。


Since Lollipop (API 21), WebView blocks all mixed content by default . 自 Lollipop (API 21) 起,WebView 默认阻止所有混合内容

To change this behaviour, when you are targeting API level 21 or above , use:要更改此行为,当您针对 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.在这种模式下,WebView 将尝试与现代 Web 浏览器在混合内容方面的方法兼容。 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.在实践中,这应该允许加载图像、视频、音乐等——所有内容在被恶意第三方篡改/替换时成为主要安全威胁的可能性很小。


Alternatively use (strongly discouraged) :或者使用(强烈不鼓励)

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.在这种模式下,WebView 将允许安全源从任何其他源加载内容,即使该源是不安全的。 This is the least secure mode of operation for the WebView, and where possible apps should not set this mode.这是 WebView 最不安全的操作模式,在可能的情况下,应用程序不应设置此模式。

If your min API is less than 21 and cannot call setMixedContentMode directly, you can use reflection:如果你的 min API 小于 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);
}

In android pie in addition to setting the mixed content mode, you also need to set the android:usesCleartextTraffic attribute in the AndroidManifest .在android pie中除了设置混合内容模式外,还需要在AndroidManifest设置android:usesCleartextTraffic属性。

In your AndroidManifest.xml do:在您的AndroidManifest.xml执行以下操作:

<application
    ....
    android:usesCleartextTraffic="true"
    ...>

and when setting up the webview, do:在设置 webview 时,请执行以下操作:

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

to load it conditionally on API >= 21, you don't have to use reflection .要在 API >= 21 上有条件地加载它,您不必使用反射

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   
          webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}

I've recently migrated from Crosswalk to use the native WebView.我最近从 Crosswalk 迁移到使用本机 WebView。

Had to fight with this issue for a few hours.不得不与这个问题斗争了几个小时。 The fix was to run clearCache() prior to setting the settings.修复方法是在设置设置之前运行 clearCache()。

webView.clearCache(false);  // <-- DO THIS FIRST
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);

Go to manifest.xml and add the following line.转到 manifest.xml 并添加以下行。

android:usesCleartextTraffic="true"

And in Java file of webview add this code.并在 webview 的 Java 文件中添加此代码。

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

If you are running into this problem, just make sure you have installed the Ionic's WebView Cordova Plugin ( https://github.com/ionic-team/cordova-plugin-ionic-webview ).如果您遇到此问题,请确保您已安装 Ionic 的 WebView Cordova 插件 ( https://github.com/ionic-team/cordova-plugin-ionic-webview )。 The easiest way is to check your package.json.最简单的方法是检查您的 package.json。

Once installed:安装后:

  1. Open your config.xml file打开你的 config.xml 文件

  2. Check if you have an entry for <preference name="Scheme">检查您是否有<preference name="Scheme">的条目

  3. If you do, check that the value is "https".如果这样做,请检查该值是否为“https”。

  4. If you don't have it, then add this line: <preference name="Scheme" value="https" />如果没有,请添加以下行: <preference name="Scheme" value="https" />

  5. Add this line: <preference name="MixedContentMode" value="0" />添加这一行: <preference name="MixedContentMode" value="0" />

This solved the problem for me.这为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM