Graeme Hancock
Graeme Hancock

Reputation: 3

Problem posting data from my app to local api using nativescript angular 6 on a device

When I post data from my native script angular app which will be running on my android device to my local API which is using dotnet core API. I get an error Error: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 59521): connect failed: ECONNREFUSED. I can hit my API using postman.

I change from using the Httpclient module to HTTP common, with no luck and I have added android:usesCleartextTraffic="true" to the android manifest.

login(user: User) {
    return this.http.post(
      BackendService.baseUrl + "user/login",
      JSON.stringify({
        username: user.email,
        password: user.password
      }),
      { headers: this.getCommonHeaders() }
    )
    .pipe(
      tap((data: any) => {
        BackendService.token = data._kmd.authtoken;
      }),
      catchError(this.handleErrors)
    );
  }

I expect to get an success but I get Error: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 59521): connect failed: ECONNREFUSED.

Upvotes: 0

Views: 905

Answers (2)

Mandolf0
Mandolf0

Reputation: 11

Today I found out you have to use 10.0.2.2 as your endpoint in order to reach the localhost from the emulator.

Question asked about running on the device, yes. But people land here for other keywords too.

Upvotes: 0

Tim
Tim

Reputation: 222

In order to connect your Android Emulator (or USB-connected Android device) to your dev server, you need to supply the IP-address of the server, since localhost will not point to it from your Emulator/Device.

Upvotes: 2

Related Questions