'Getting isConnected failed: ECONNREFUSED (Connection refused) error for Kotlin Retrofit post request
I am running a Sample Spring Boot app on my local machine server (localhost) on port 8080. From client app, I was trying to make a post request through Kotlin Retrofit, in my Android Studio Emulator,
Kotlin Code
interface RestApi {
@Headers("Content-Type: application/json")
@POST("users")
fun addUser(@Body userData: CTUserInfo): Call<ResponseBody>
}
object ServiceBuilder {
private val client = OkHttpClient.Builder().build()
private val retrofit = Retrofit.Builder()
.baseUrl("http://localhost:8080/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
fun<T> buildService(service: Class<T>): T{
return retrofit.create(service)
}
}
class RestApiService {
fun addUser(userData: CTUserInfo){
val retrofit = ServiceBuilder.buildService(RestApi::class.java)
retrofit.addUser(userData).enqueue(
object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
// failure
print("Failed") // getting t = below mentioned error
}
override fun onResponse( call: Call<ResponseBody>, response: Response<ResponseBody>) {
if (response.code() == 201) {
// user added
print("Success")
} else{
//user could not be added
print("Failed")
}
}
}
)
}
}
I am getting below error:
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080) from /127.0.0.1 (port 57452) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
However, when I am trying to post from Postman it succeeds on http://localhost:8080/users.
How to resolve it?
Update
My Android manifest files looks like,
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
...
Solution 1:[1]
have you added permission internet in your manifest? and you have to add clear teext trafic if you test on a new device whose API is Pie. like this :
<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">
Solution 2:[2]
You can try adding in res/values/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">http://localhost:8080/</domain>
</domain-config>
</network-security-config>
and reference this on manifest inside tag application
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...
</application>
Solution 3:[3]
If your local backend setup is from Window's subsystem, you have to use the OS' ip instead of localhost by using ifconfig command from terminal. Use the IP address specified for your development purposes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hedi KARRAY |
| Solution 2 | Samuel Arenas |
| Solution 3 | Jim Ovejera |
