Dayvid Kelly
Dayvid Kelly

Reputation: 124

Ionic NOT getting response from API

I'm actually building an app with the use of IONIC framework. I'm trying to get back some value from API which is working find on the web view of the ionic (ionic serve) but when i run the command ionic capacitor run android it does not work on the android version and its giving back the following error:

{"headers": {"normalizedNames":{},"lazyUpdate": null, "headers":{}},"status": 0, "statusText": "Unknown Error", "url": "https://xxx.xxx.xxx.xxx/company","ok":"false","name":"HttpErrorResponse","message" : "Http failure response for http://xxx.xxx.xxx.xxx/company: 0 Unknown Error","error": {"isTrusted":true}}listing

this is the code from the http.service.ts file:

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";

@Injectable({
  providedIn: "root",
})
export class HttpService {
  constructor(private http: HttpClient) {}

  private URL = "http://xxx.xxx.xxx.xxx/company";

  getCompany() {
    return this.http.get(this.URL).toPromise();
  }
}

and this is the code from home.page.ts:

import { HttpService } from "../services/http.service";

    this.http
      .getCompany()
      .then((response) => {
        console.log(response);
        this.companies_array = JSON.parse(JSON.stringify(response));

        this.is_loading = false;
      })
      .catch((response) => {
        this.err_msg = JSON.stringify(response);
      });

i've added the import in app.module.ts:

import { HttpClientModule } from "@angular/common/http";

And i've added a file call proxy.conf.json where i've added the following code:

{
    "/api/*": {
        "target": "http://xxx.xxx.xxx.xxx",
        "secure": false,
        "logLevel": "debug"
    }
}

and i've modify the angular.json file where :

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "app:build",
            "proxyConfig": "proxy.conf.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "app:build:production"
            },
            "ci": {
              "progress": false
            }
          }
        },

Upvotes: 0

Views: 1406

Answers (1)

Ranjeet Sharma
Ranjeet Sharma

Reputation: 76

I faced same issue, you can you fix something like that. You have to just add one line, i.e.

android:usesCleartextTraffic="true"

in your manifest file i.e.,

[yourProject]/android/app/src/main/AndroidManifest.xml

Upvotes: 1

Related Questions