CLEARTEXT communication - Android

212 Views Asked by At

Facing this Cleartext problem with my android project.
I have almost used all stackoverflow answers, various blogs etc, but android is not allowing me to connect to server. This is a very small app, will be used by 20+ ppl to upload some data on daily basis on local network. So HTTPS is a no go area for me.

Manifest version-1

    <application
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning"
        android:theme="@style/AppTheme">
        <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Manifest Version 2

<application
        android:allowBackup="true"
        android:usesCleartextTraffic="true" <!-- With and Without -->
        android:networkSecurityConfig="@xml/network_security_config"
        tools:ignore="GoogleAppIndexingWarning"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Network_Security_Config

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain> <!-- Debug port -->
        <domain includeSubdomains="true">192.168.0.104</domain>
    </domain-config>
</network-security-config>

I also tried creating a minSdk 21 and target sdk 24 project and it was having same problem using HTTPUrlConnection
Please Guide . Thanks.

Some Links i've checked
Retrofit CLEARTEXT communication not enabled for client
How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?
https://developer.android.com/about/versions/pie/android-9.0-changes-all
Internet permission not working in oreo and pie

1

There are 1 best solutions below

3
On

Have you ever tried to add this to your manifest application,

    android:usesCleartextTraffic="true"

instead of

android:networkSecurityConfig="@xml/network_security_config"