Skip to content
Discussion options

You must be logged in to vote

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, localhost or 127.0.0.1 refers to the emulator itself, not your development machine.

Solution

For Android Emulator

Use 10.0.2.2 instead of localhost. Update your Retrofit base URL:

// 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()
}

10.0.2.2 is the special alias the Android emulator uses to reac…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Pixell-droid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants