11

I'm trying to make a post request to an http server, but when I try to get an input stream I get the error java.io.IOException: Cleartext HTTP traffic to x not permitted

I've already tried putting android:usesCleartextTraffic="true" in my manifest, as well as making a network security config and setting the android:targetSandboxVersion to 1

app/src/main/res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">Server adress</domain>
    </domain-config>
</network-security-config>

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.packagename"
    android:targetSandboxVersion="1">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config"
        >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Logcat output

D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
W/System.err: java.io.IOException: Cleartext HTTP traffic to x not permitted
W/System.err:     at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:124) 
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)

Any help would be appreciated

Reynard
  • 111
  • 1
  • 1
  • 4
  • 1
    Possible duplicate of [Android 8: Cleartext HTTP traffic not permitted](https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted) – Pratik Butani Sep 20 '19 at 07:19

9 Answers9

14

Try using just

android:usesCleartextTraffic="true"

delete this 2

android:targetSandboxVersion="1"
android:networkSecurityConfig="@xml/network_security_config"

mine is working in any API just using android:usesCleartextTraffic="true"

Boken
  • 4,825
  • 10
  • 32
  • 42
L2_Paver
  • 596
  • 5
  • 11
3

Create a file in your project

res/xml/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/security_config"
        ...>
        ...
    </application>
</manifest>

Also if you have android:targetSandboxVersion in then reduce it to 1

Boken
  • 4,825
  • 10
  • 32
  • 42
sushil
  • 31
  • 3
  • Edited my original question to make it clearer that I've already tried all of that – Reynard Sep 20 '19 at 06:34
  • If above solution is not working, try changing your url's from HTTP to HTTPS it will also solve your issue. – sushil Sep 20 '19 at 07:06
3

If you are working in your local machine make sure that you are requesting to 10.0.2.2 instead of localhost (this is very important)

Then make sure that your AndroidManfest.xml file has following lines:

<uses-permission android:name="android.permission.INTERNET" />

<application
        ... 
        android:networkSecurityConfig="@xml/network_security_config">

and your network_security_config.xml file must look like this:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

or if you need to allow request to specific domains you could modify network_security_config.xml file with:

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

Both network_security_config.xml configurations works for me. I'm working on my local development enviroment.

3

Don't do like this way it may be get run time error To Do easiest method; go to android native project here you can see Properties click there then take AssemblyInfo.cs file then edit this part below

[assembly: Application(UsesCleartextTraffic = true)]

example below my code:-

 using System.Reflection;
    using System.Runtime.CompilerServices;

    using System.Runtime.InteropServices;

    using Android.App;

    // General Information about an assembly is controlled through the following 
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("ZATWEBO.Android")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("ZATWEBO.Android")]
    [assembly: AssemblyCopyright("Copyright ©  2014")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    [assembly: ComVisible(false)]

    // Version information for an assembly consists of the following four values:
    //
    //      Major Version
    //      Minor Version 
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Build and Revision Numbers 
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]

    // Add some common permissions, these can be removed if not needed
    [assembly: UsesPermission(Android.Manifest.Permission.Internet)]
    [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
    [assembly: Application(UsesCleartextTraffic = true)]

Note: this change only need android 9 Pie or higher versions

1

I added the following code to the Android Manifest file:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config>
            <trust-anchors>
                <certificates src="system">
                </certificates>
            </trust-anchors>
        </base-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">http://YourURL/api/</domain>
        </domain-config>
    </network-security-config>
sonique
  • 4,539
  • 2
  • 30
  • 39
Aravindh
  • 11
  • 3
0

May be you made a mistake. Here is a correct format got it from another source.

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

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>

If you have android:targetSandboxVersion in then reduce it to 1

AndroidManifest.xml -

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

Suman Kumar Dash
  • 681
  • 5
  • 19
0

Solved my own problem, airing my shame for posterity.

Accidentally put a url in <domain includeSubdomains="true"></domain> instead of a domain.

Reynard
  • 111
  • 1
  • 1
  • 4
0

I had the same problem and I solved it by lowering the android version to 25 in And, the application works well on devices with android 9 and smaller.

<uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="25" />
-2

Make sure you have the https request for both post and origin header. If you have one http request and the other is htps:// it won't work I had the same problem with the same error.