Fix cleartext HTTP traffic not permitted in Android

There is an error when you try to connect to a HTTP server.

Cannot send data to the server java.net.UnknownServiceException: CLEARTEXT communication to [HOST] not permitted by network security policy

Starting from Android 8, there is a special security policy which requires connection via HTTPS protocol instead of HTTP. So, if you use HTTP, the system shows the error.

There are 2 methods to address this issue.

One is to use HTTPS instead HTTP. Nowadays, most online services is secured behind SSL so it is normal to use HTTPS.

The other method is to add an exception to the security policy.

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

Set android:usesCleartextTraffic to true allows HTTP connection.

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close