Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http.get() working but not working in build(Release/Debug) in Ionic 4

I am trying get data from a simple api, it works fine in ionic serve(browser) , But when i build the app http call does not work. my Code is

this.http.get("http://example.com/api/routes").subscribe(response => {
 this.routes = response["routes"];
 for (let x in this.routes) {
 let a = this.routes[x].rou_stops;
 let b = a.split(",");
          for (let y in b) {
            this.newCit.push(b[y]);

          }
        }
   });

please help with this issue.

like image 222
Dinesh Kumar Avatar asked Dec 12 '19 14:12

Dinesh Kumar


3 Answers

I am Guessing that your are getting this because of android changes its http architecture.

to make it working on Android go to your project root folder.

yourAppFolder > resources > android > xml > network_security_config.xml Change your network security config to blow code.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>
like image 95
Najam Us Saqib Avatar answered Oct 17 '22 19:10

Najam Us Saqib


IONIC 5 UPDATE

I had the same issue in Ionic 5 but made more complicated by the fact that HttpClient was not catching the error (I was simply getting back an unknown failed response).

In order to see that it was indeed a cleartext error I first had to change HttpClient for the Native HTTP plugin.

In Ionic 5 you can now enable cleartext in the capacitor.config.json file or replace the existing json file with a .ts version:

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
                 appId: 'com.company.appname',
                 appName: 'My Capacitor App',
                 webDir: 'www',
                 server: {
                           'cleartext': true
                         }
              };

export default config;

docs

like image 25
jamesbcn Avatar answered Oct 17 '22 19:10

jamesbcn


This works for me on the Ionic 4 app.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>
like image 42
Sampath Avatar answered Oct 17 '22 21:10

Sampath