emulator connection refused errors #130
-
|
How do I connect the Android app to the backend API? I keep getting connection refused errors. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
This is usually caused by the Android emulator not being able to reach The IssueWhen running in the Android emulator, SolutionFor Android EmulatorUse // In your network configuration
object ApiClient {
private const val BASE_URL = "http://10.0.2.2:8080/api/"
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
}
For Physical DeviceIf testing on a real device over USB: # Forward the port via ADB
adb reverse tcp:8080 tcp:8080Then you can use For DockerIf your backend runs in Docker, make sure the port is exposed: docker-compose up -d
# API is at http://localhost:8080/api from your machine
# Use http://10.0.2.2:8080/api from the emulatorCheck the AndroidManifest.xmlEnsure you have internet permission: <uses-permission android:name="android.permission.INTERNET" />And if using HTTP (not HTTPS), add |
Beta Was this translation helpful? Give feedback.
This is usually caused by the Android emulator not being able to reach
localhost. Here's how to fix it:The Issue
When running in the Android emulator,
localhostor127.0.0.1refers to the emulator itself, not your development machine.Solution
For Android Emulator
Use
10.0.2.2instead oflocalhost. Update your Retrofit base URL:10.0.2.2is the special alias the Android emulator uses to reac…