为什么使用明文传输的HTTP流量会持续出现ERR_CLEARTEXT_NOT_PERMITTED错误?

6

我正在使用Ionic框架开发一个新的应用程序,并使用HttpClient模块进行API请求。

问题在于,我已经阅读并尝试实施以下解决方案:

  1. https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
  2. https://forum.ionicframework.com/t/livereload-err-cleartext-not-permitted/163487
  3. Android 8:不允许明文HTTP流量
  4. 为什么升级到Cordova Android 8后,我看到net::ERR_CLEARTEXT_NOT_PERMITTED错误?
  5. 如何在flutter中修复'net::ERR_CLEARTEXT_NOT_PERMITTED'
  6. Android Pie:WebView在某些网站上显示明文HTTP错误,即使使用usesClearTextTraffic="true"
  7. WebView显示ERR_CLEARTEXT_NOT_PERMITTED,尽管站点是HTTPS

但是,当我的应用程序对API进行查询时,它仍然会抛出此错误。

以下是我的文件的详细信息:

/处,config.xml文件如下:

    <?xml version='1.0' encoding='utf-8'?>
<widget id="com.example" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    ...
    <platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config"/>
            <application android:usesCleartextTraffic="true" />
        </edit-config>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
     ...
</widget>

/resources/android/xml/路径下,存在 network_security_config.xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
      <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

不能使用安全加密协议HTTPS,该API仅支持HTTP。


我也遇到了同样的问题。在Android Pie中,不允许使用Http API作为默认选项。因此,在除Android Pie之外的所有其他设备上都可以工作。我正在努力寻找解决方法,如果找到了解决方案,将会更新您。如果您找到了可行的解决方案,请告诉我。 - Gvs Akhil
可能是[Android 8:不允许明文HTTP流量]的重复问题(https://dev59.com/w1cO5IYBdhLWcg3wWgU0)。 - Zoe stands with Ukraine
2个回答

11

更新于2019年12月 ionic - 4.7.1

<manifest xmlns:tools=“http://schemas.android.com/tools”>

<application android:usesCleartextTraffic=“true” tools:targetApi=“28”>

请在Android Manifest .xml文件中添加上述内容。

旧版ionic

  1. 确保您的Ionic项目的config.xml中有以下内容:

<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config" />
            <application android:usesCleartextTraffic="true" />
        </edit-config>
  • 运行 ionic Cordova build android。它会在 Platforms 下创建 Android 文件夹。

  • 打开 Android Studio 并打开项目中的 project-platforms-android 中的 Android 文件夹。等待几分钟,以使其构建 gradle。

  • gradle build 完成后,我们会在 manifest.xml 中包含 minSdVersion 出现一些错误。 现在我们要做的就是从 manifest.xml 中删除 <uses-sdk android:minSdkVersion="19" />

    确保它从以下两个位置中删除:

    1. app → manifests → AndroidManifest.xml
    2. CordovaLib → manifests → AndroidManifest.xml

    现在再次尝试构建 gradle,现在它会成功构建

  • 请确保在 App → manifest → Androidmanifest.xml 的应用程序标记中拥有以下内容:

  • <application
    android:networkSecurityConfig="@xml/network_security_config"  android:usesCleartextTraffic="true" >
    
  • 打开network_security_config.xml文件(路径为app → res → xml → network_security_config.xml)。

    添加以下代码:

  • <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">xxx.yyyy.com</domain>
        </domain-config>
    </network-security-config>
    

    这里的xxx.yyyy.com是您的HTTP API链接。请确保在URL之前不包括任何Http。

    注意:现在使用Android Studio构建应用程序(Build--Build Bundle's/APK--Build APK),现在您可以使用该应用程序,在Android Pie上运行良好。如果您尝试使用ionic Cordova build android构建应用程序,它将覆盖所有这些设置,因此请确保使用Android Studio构建项目。

    如果您安装了任何旧版本的应用程序,请卸载它们并尝试一下,否则您将留下一些错误:

    App not Installed


    我的解决方法是将 network_security_config 设置为 这适用于 Ionic 5。 - Pablo Garcia
    @PabloGarcia,你在哪个文件中进行了这些更改? - Tijl .Reynhout

    1
    1. 请确保在Ionic项目的config.xml文件中包含以下内容:

      <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:usesCleartextTraffic="true" android:networkSecurityConfig="@xml/network_security_config" /> </edit-config>

    2. 在 /resources/android/xml/ 目录下,创建 network_security_config.xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
     <base-config cleartextTrafficPermitted="true"/> 
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">localhost</domain>
      <domain includeSubdomains="true">www.apiurl.com</domain>
        </domain-config>
    </network-security-config>
    

    请注意,在域名 URL 中不要添加 http。
    在 Ionic 4 的 HTTP API 调用中,对我非常有效。

    有人可以回答一下这个问题吗?https://stackoverflow.com/questions/59116787/failed-to-load-resource-neterr-cleartext-not-permitted - pratik jaiswal
    未在/resources/android/xml/找到network_security_config文件。 - Shashwat Gupta
    然后在配置 XML 中创建并链接它。 - santhosh

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接