Ionic Native HTTP 不适用于 Android 9 (Pie) 及更高版本?

Ionic Native HTTP does not work for Anroid 9 (Pie) and up?

我创建了一个应用程序,它使用 Ionic Native HTTP 成功连接到我们的服务器,并使用 Samsung Galaxy J7 Prime OS 版本 Marshmallow 和其他几个尚未在 Android Pie 下进行测试.出于某种原因,当我使用 Android Pie 下的设备对其进行测试时,它不再工作并且我收到 CORS 策略问题。有人遇到过类似的问题吗?有解决方法吗?不幸的是,不能在服务器配置中进行修改。我还阅读了一些有关代理的解决方案,但不确定如何实施它们。

版本: cordova-plugin-advanced-http: 2.1.1 ionic-native/http: 5.8.0

Android P 默认需要 HTTPS。这意味着如果您在您的应用程序中使用未加密的 HTTP 请求,该应用程序将在所有低于 Android P.

的版本中正常运行

为避免此安全异常,请尝试对您的应用代码进行以下更改。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config"
                    ... >
        ...
    </application>
</manifest>

并在 res/xml 中添加名为:network_security_config.xml

的文件

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        //  Add host of your download URL in below line. 
        //   ie. if url is  "https://www.google.com/search?source=...."
        //   then just add "www.google.com"
        <domain includeSubdomains="true">www.google.com</domain>
    </domain-config>
</network-security-config>

要解决该问题,您应该更改 config.xml 文件。

您需要更改的第一件事是 'widget' 标记,它看起来应该与此类似:

<widget id="com.dynamicsoft.app" version="2.2.2" xmlns="http://www.w3.org/ns/widgets" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:cdv="http://cordova.apache.org/ns/1.0">

之后,在 name='android' 的平台内部,您应该添加此代码:

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
    <application android:usesCleartextTraffic="true" />
</edit-config>

之后您可以运行重建应用程序。