Post method rest API in ionic3 not working in Android device but working in ionic serve

Rest API post method i was used in my app ionic serve working properly but when i was run to android i will not showing the result .If i am using

ionic cordova run android -l -c this livereload option it will be working only problem is when i was running in the android device it will not working how can i solve this issue…
any solution thanks …!

What does the request look like?

Are you using an Ionic Proxy?

1 Like

My requst.ts file:

 logindetails()
  {
     let headers = new Headers();
      headers.append("Accept",'application/json');
   headers.append('content-type','application/json');
        let options = new RequestOptions({ headers: headers});
    let postParams = 
      {
        "latitude": 11.0168,
        "longitude": 76.9558,
        "location": "cbe"
      }
     return this.http.post(this.url, JSON.stringify(postParams),options).map(this.extractData);
      }

ionic.config.json file:


 "proxies": [
    {
      "path": "/api",
      "proxyUrl": "http://192.168.0.17/sso/public/api"
    }

No @d6hl3tn i used my proxy

Hi, @sabarinathen

First add platform (android or ios) and build you app

Then using below mentioned code will help you in your functionality,

Thanks

I follow the above link step by step but again the same problem when i was run on android result will not be showing but ionic serve working perfectly

Homepage.ts

export class HomePage {
 public locat: any;
   public datas: any;

  public postParams =
  {
    "latitude": 11.0168,
    "longitude": 76.9558,
    "location": "cbe"
  }
  constructor(public navCtrl: NavController, private httpprovider: HttpProvider) {
    this.dologin();
  }

  dologin() {
    this.httpprovider.log(this.postParams).then((result) => {
      this.datas = result;
      console.log("this.datasss", this.datas);
      this.locat = this.datas.data.location;
    
    }, (err) => {
     
      console.log("errorrr");

    });
  }


}

Http provider.ts file

export class HttpProvider {
  private url: string = "api/add-location";
  constructor(public http: Http) {
    
  }
  log(credentials) {
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');

      this.http.post(this.url, JSON.stringify(credentials), { headers: headers })
        .subscribe(res => {
          resolve(res.json());
        }, (err) => {
          reject(err);
        });
    });
  }

}

ionic.config.json file

{
  "name": "httppost",
  "app_id": "",
  "type": "ionic-angular",
  "integrations": {
    "cordova": {}
  },
  "proxies": [
    {
      "path": "/api",
      "proxyUrl": "http://192.168.0.17/sso/public/api"
    }
  ]
}

anything missing? how can i solve these issue @addwebsolution

If you are getting any issue while running the code on android device then put that error here, you can always put alert for getting the errors.

This is the error only i received when i was inspect chrome://inspect @addwebsolution. There is no error in ionic serve and browser console

This is antipattern soup. You do not need to be creating headers. You do not need to be manually stringifying objects. You do not need to be setting content type. You should not be creating Promises. You should not be subscribing in the same place you are generating an Observable. You should be declaring types for all parameters and function return values.

could you please give some samples how to do @rapropos

1 Like

Meaby is a little late, but did you get an answer to this problem? I have the same issue.
Thank you

I have same issue in Android, rest api not working in android emulator or device and give error: Response with 0 for Url: null.
But finally i got solution that there is cordova platform android version issue, if there is cordova Android version is 6.3 then it’s working properly but it there is cordova Android version is 7.0 or 7.1.1 then it’s not working for me.
You can check ios or android version using cli command.
ionic info

Also check your all cordova plugins is installed or not. If someone is missing then install it manually and check again.
You can check your cordova plugins using cli command.
cordova plugin -ls
I think it is helpful you.

Thanks

http://ivancevich.me/articles/ignoring-invalid-ssl-certificates-on-cordova-android-ios/ seems to be a solution