添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

ionic 4 app sends http request for android version 6,7 but gives an error saying CLEARTEXT http traffic (not permitted). I have trying to solve this by adding android:usesCleartextTraffic="true"
but ionic default is true for usesCleartextTraffic.

and also i tried to change url http to https. but nothing works for me.

so i solved this issue temporarily by adding

<domain includeSubdomains="true">localhost</domain> <domain includeSubdomains="true">YOUR-IP_ADDRESS</domain>

to my network_security_config.xml .

localhost for android version 6,7
YOUR_IP_ADDRESS for version 8,9

Please find the details below

  • added this in resources/android/xml/network_security_config.xml
  • <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
    <base-config cleartextTrafficPermitted="true">
    <trust-anchors>
    <certificates src="system" />
    </trust-anchors>
    </base-config>
    <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">ourdomain.com</domain>
    <domain includeSubdomains="true">subdomain.ourdomain.com</domain>
    </domain-config>
    </network-security-config>
    2) added in config.xml <platform name="android">
    allow-intent href="market:*" />
    <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" />
    <application android:networkSecurityConfig="@xml/network_security_config" />
    </edit-config>
    <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/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 includeSubdomains="true">abcd.com</domain>
        </domain-config>
    </network-security-config>
    

    Try with this code use your domain in abcd.com , do not change the localhost

    I am working on ionic 5 using capacitor. I tried to call an Larvel api from localhost but am getting this error

    First I got the below error
    There was an error with the request: Cleartext HTTP traffic to localhost not permitted ionic

    Fixed doing below in AndroidManifest.xml

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
    

    Error changes
    There was an error with the request: Failed to connect to localhost/127.0.0.1:80

    I did not see any file in ionic 5 with the name:
    network_security_config

    Ionic Info:

    Ionic CLI : 6.11.11 (C:\Users\Ahmad Raza Mughal\AppData\Roaming\npm\node_modules@ionic\cli)
    Ionic Framework : @ionic/angular 5.3.5
    @angular-devkit/build-angular : 0.1000.8
    @angular-devkit/schematics : 10.0.8
    @angular/cli : 10.0.8
    @ionic/angular-toolkit : 2.3.3

    Capacitor:

    Capacitor CLI : 2.4.2
    @capacitor/core : 2.4.2

    Utility:

    cordova-res : 0.14.0
    native-run : 1.0.0

    System:

    NodeJS : v12.16.2 (C:\Program Files\nodejs\node.exe)
    npm : 6.14.4
    OS : Windows 10

    Please help with the above?

    Thanks