How to Solve Cleartext HTTP traffic not permitted Error in Android Studio

In this article, we will learn how to Solve Cleartext HTTP traffic not permitted Error in Android Studio. You will see the four best possible solutions to solve this error.

You are facing the following exception in your console log.

08-29 12:03:11.246 11285-11285/ E/: [12:03:11.245, main]: Exception: IOException java.io.IOException: Cleartext HTTP traffic to * not permitted
at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.deiw.android.generic.tasks.AbstractHttpAsyncTask.doConnection(AbstractHttpAsyncTask.java:207)
at com.deiw.android.generic.tasks.AbstractHttpAsyncTask.extendedDoInBackground(AbstractHttpAsyncTask.java:102)
at com.deiw.android.generic.tasks.AbstractAsyncTask.doInBackground(AbstractAsyncTask.java:88)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

Depending on how the network security config of Android 9 (API level 28) is configured, the plain text support is deactivated by default.

Option 1

If your server has a valid SSL certificate then try to change the URL “http: //” to “https: //”.

Option 2

Add the android:usesCleartextTraffic inside your application in your manifest file, which helps to solve and understand the HTTP traffic error.

AndroidManifest.xml File

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

Option 3

Create the file res / xml / network_security_config.xml.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
    </domain-config>
</network-security-config>

AndroidManifest.xml File

<?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 4

If you have android:targetSandboxVersion in the <manifest> then reduce it to 1.

AndroidManifest.xml File

<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
    <uses-permission android:name="android.permission.INTERNET" />
    ...
</manifest>

Also android:targetSandboxVersion can be a problem as well.

  • The target sandbox that this app should use. if the sandbox version number is higher then the security level must be higher. The default value is 1. You can also set it to 2. If you set this attribute to 2, the application switches to another SELinux sandbox. At level 2 sandbox the following restrictions applying.
  • The default value for usesCleartextTraffic in the network security configuration is false.
  • UID sharing is not allowed.

That’s ass, we will learn how to Solve Cleartext HTTP traffic not permitted Error in Android Studio.

Next Article

1. How to Iterate Through Java MAP in Android
2. How to Use Java Ternary Conditional Operator in Kotlin
3. How to Create a Static Object in Kotlin Android
4. How to assign Background to a View with Transparency in Android

Please share this post:
Posts created 64

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: