Reputation: 40196
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,
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?
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">
...
Upvotes: 0
Views: 4183
Reputation: 876
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.
Upvotes: 0
Reputation: 40196
Finally I resolved the issue by adding actual machine IP
found by running below commands in bash
ifconfig
ipconfig /all
Example,
private val retrofit = Retrofit.Builder()
.baseUrl("http://192.168.64.1:8080/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
Upvotes: 2
Reputation: 1
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>
Upvotes: 0
Reputation: 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">
Upvotes: 0