Search code examples
androidionic-frameworkcapacitor

Ionic/Capacitor net::ERR_CONNECTION_REFUSED when Android calling API


I have an ionic vue app that I want to test as an android app.

I use following commands to create the app:

ionic cap add android
ionic build
ionic cap copy
ionic cap open android

Then I start the app on a virtual device in android studio. I use android API Level 30.

I get an ERR_CLEARTEXT_NOT_PERMITTED error when calling my django api (which I started locally, that's why there is no https).

I already tried adding this to my capacitor.config

server: {
  cleartext: true
}

This results in another error message: net::ERR_CONNECTION_REFUSED.

I already tried various other things like suggested here: Ionic 5/Capacitor ERR_CLEARTEXT_NOT_PERMITTED in Android, but none helped so far.

When I use an Android version before API level 28 I get the net::ERR_CONNECTION_REFUSED right away


Solution

  • I solved it by just adding a condition in my axios config:

    const host = isPlatform('android') ? '10.0.2.2' : 'localhost'