free geoip
36

Kotlin App Not Connecting to Internet on Emulator

If your Kotlin app is not connecting to the internet on the Android emulator, you’re not alone. This is a…

If your Kotlin app is not connecting to the internet on the Android emulator, you’re not alone. This is a common issue faced by many developers during the testing phase in Android Studio. Fortunately, the solution is straightforward once you understand the causes and know where to look.

Kotlin app not connecting to internet

Common Reasons the Emulator Can’t Connect

  1. Missing Internet Permission: Android apps need explicit permission in the manifest to access the internet.
  2. Wrong Network Configuration: The emulator might be running in a virtual network environment with no proper DNS or proxy.
  3. Incorrect API Endpoint: Using localhost instead of the emulator’s host IP (10.0.2.2).
  4. Firewall or Antivirus Restrictions: Local security software may block emulator traffic.

Step-by-Step Solution

1. Add Internet Permission

First, ensure you have added the correct permission in your AndroidManifest.xml:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>

2. Use Correct Base URL for Emulator

When your backend server is running on your local machine, never use localhost or 127.0.0.1. Android emulator uses 10.0.2.2 to access your host machine.

Example Retrofit Service:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// File: ApiService.kt
interface ApiService {
@GET("/data")
suspend fun getData(): Response<MyData>
}
// File: ApiService.kt interface ApiService { @GET("/data") suspend fun getData(): Response<MyData> }
// File: ApiService.kt
interface ApiService {
    @GET("/data")
    suspend fun getData(): Response<MyData>
}

Retrofit Client:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// File: ApiClient.kt
object ApiClient {
private const val BASE_URL = "http://10.0.2.2:8080"
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
// File: ApiClient.kt object ApiClient { private const val BASE_URL = "http://10.0.2.2:8080" val retrofit: Retrofit = Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build() }
// File: ApiClient.kt
object ApiClient {
    private const val BASE_URL = "http://10.0.2.2:8080"

    val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
}

3. Check Emulator Network Settings

  • Go to Settings > Network & Internet on the emulator.
  • Make sure airplane mode is off and the emulator has an IP address.
  • If needed, restart the emulator or cold boot it from AVD Manager.

4. Check Local Server Accessibility

Ensure your local server is actually running and listening on the expected port (8080 in the above example). You can test using:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
curl http://localhost:8080
curl http://localhost:8080
curl http://localhost:8080

Or try accessing http://10.0.2.2:8080 in the emulator browser.

5. Debug With Logs

Use Logcat to check for UnknownHostException, SocketTimeoutException, or permission errors.

Final Tips

  • Avoid using HTTPS without a valid certificate for local dev.
  • For APIs hosted online, test with real device if possible.
  • You can learn more about emulator networking in official Android docs.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *