Cleartext Http Traffic not Permitted Android 9

Android Studio Error Log: java.io.IOException: Cleartext HTTP traffic to my_IP/domain not permitted

2019-09-06 12:20:29.050 22531-22531/com.geekscompete.gate_isro_cs.ugc_net_preparation E/VolleyError:: ::com.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to http://geekscompete.com/ not permitted

Reason for this error:
According to Network security configuration - Opt out of cleartext traffic
Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. Starting with Android 9 (API level 28), cleartext support is disabled by default. Network security configuration also suggest to ensure that all connections to your domain are always done over HTTPS to protect sensitive traffic from hostile networks.

You have to create a new file in your xml folder, file named network_security_config.xml.
res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">yourdomain.com</domain>
        <domain includeSubdomains="true">geekscompete.com</domain>
        <domain includeSubdomains="true">geekscompete.blogspot.com</domain>
    </domain-config>
</network-security-config>

This config will now allow clear traffic to your specified domains after you . Make sure that you have set android:networkSecurityConfig="@xml/network_security_config" in the application tag of your AndroidManifest.xml. The content of your config file should contain all URLs which requests clear traffic without encryption. As shown in below code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.yourappname">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".MainApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:allowBackup="false"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:theme="@style/AppTheme">
    </application>
</manifest>


Related Articles: 


No comments:

Post a Comment

Popular Posts