sir-haver
sir-haver

Reputation: 3592

Cleartext HTTP traffic to my localhost is not permitted

I'm using Volley for a GET request to an address on my localhost, but it fails with the error:

Cleartext HTTP traffic to 192.168.1.45 not permitted

I followed the guide here: Android 8: Cleartext HTTP traffic not permitted And did the following:

Created the network security xml file:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://192.168.1.45/companyweb/greetings</domain>
    </domain-config>
</network-security-config>

Added it in my manifest and also allowed cleartext traffic:

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

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

    <application
        android:networkSecurityConfig="@xml/network_security_config"
        android:usesCleartextTraffic="true"

I'm still getting the same error. Changing from 'http' to 'https' gives this error:

socket failed: EPERM (Operation not permitted)

My code in MainActivity:

val url = "http://192.168.1.45/companyweb/greetings"
val rq = Volley.newRequestQueue(this)
val sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
    fragmentText.text = response
    Log.d("TAG", "success")
}, Response.ErrorListener { error -> Log.d("TAG", "fail" + error.toString()) })
rq.add(sr)

What can I do next?

Upvotes: 1

Views: 4574

Answers (3)

Callum Patterson
Callum Patterson

Reputation: 41

I had a similar problem in a recent project, try adding:

android:usesCleartextTraffic="true"

To your AndroidManifext.xml file under the application tag.

Upvotes: 1

sir-haver
sir-haver

Reputation: 3592

I found the answer here: java.net.SocketException: socket failed: EPERM (Operation not permitted)

Apparantly what I had to do is restart the emulator (or uninstall the app)

Upvotes: 0

Ryan M
Ryan M

Reputation: 20137

You should include only the IP address, that is:

<domain includeSubdomains="true">192.168.1.45</domain>

Upvotes: 3

Related Questions