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

2020-04-05 09:30发布

问题:

I've created an app that connects successfully to our server using Ionic Native HTTP, testing it with Samsung Galaxy J7 Prime OS version Marshmallow and several others that were not yet under Android Pie. For some reason, when I tested it with a device under Android Pie, it won't work anymore and I'm receiving CORS policy issues. Has anybody encountered a similar issue? Is there a workaround for this? Unfortunately, modifications within the server config is not an option. I've also read some solutions about proxy but am not sure how to implement them.

Versions: cordova-plugin-advanced-http: 2.1.1 ionic-native/http: 5.8.0

回答1:

Android P requires HTTPS by default. What this means is that if you are using unencrypted HTTP requests in your app, the app will work fine in all lower versions than Android P.

To avoid this security exception, try below changes in your app code.

In AndroidManifest.xml

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

and in res/xml add file named : 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>


回答2:

To solve that problem, you should make changes to the config.xml file.

First thing you need to change is 'widget' tag, it should look similar to this one:

<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">

After that, inside of platform where name='android', you should add this code:

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

After that you can run rebuild the application.