Android 8,9: Cleartext HTTP traffic not permitted ,JSON ERROR

Jyotishgher Astrology
By -
0

Cleartext is any transmitted or stored information that is not encrypted or meant to be encrypted.


<uses-library android:name="org.apache.http.legacy" android:required="false"/>
java.io.IOException: Cleartext HTTP traffic to * not permitted
JSON ERROR

When an app communicates with servers using a cleartext network traffic, such as HTTP, it could raise a risk of eavesdropping and tampering of content. Third parties can inject unauthorized data or leak information about the users. That is why developers are encouraged to a secure traffic only, such as HTTPS.

Fix Cleartext Traffic Error in Android 9 Pie

I had reports from users with Android 8 that my app (that uses back-end feed) does not show content. After investigation I found following Exception happening on Android 8 and 9:


SOLUTIONS:
Android 6.0 introduced the useCleartextTraffic attribute under application element in android manifest. The default value in Android P is “false”. Setting this to true indicates that the app intends to use clear network traffic.

Starting with Android 9 (API level 28), cleartext support is disabled by default.


Option 1 -

Create file res/xml/network_security_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain>
    </domain-config>
</network-security-config>
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        ...>
        ...
    </application>
</manifest>
Option 2 -
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

Post a Comment

0Comments

Post a Comment (0)