Simple Post Request to my URL gives me "Unable to resolve host"


#1

I am trying to make a simple POST request over the HTTP Module and gets this error:


JS: {"originalStack":"Error: java.net.UnknownHostException: Unable to resolve host \"yourwebsite.com\": No address associated with hostname\n    at ZoneAwareError (file:///data/data/org.nativescript.YourApp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:994:33)\n    at onRequestComplete (file:///data/data/org.nativescript.YourApp/files/app/tns_modules/tns-core-modules/http/http-request/http-request.js:45:34)\n    at Object.onComplete (file:///data/data/org.nativescript.YourApp/files/app/tns_modules/tns-core-modules/http/http-request/http-request.js:37:13)","zoneAwareStack":"Error: java.net.UnknownHostException: Unable to resolve host \"yourwebsite.com\": No address associated with hostname\n    at onRequestComplete (file:///data/data/org.nativescript.YourApp/files/app/tns_modules/tns-core-modules/http/http-request/http-request.js:45:34) [<root>]\n    at Object.onComplete (file:///data/data/org.nativescript.YourApp/files/app/tns_modules/tns-core-modules/http/http-request/http-request.js:37:13) [<root>]","__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0}}

The Code:

 login(user: User) {
    return this.http.post(
      encodeURI("http://yourwebsite.com/login/"),
       JSON.stringify({
        format: 'json',
        email: user.email,
        password: user.password
      }),
      { headers: this.getCommonHeaders() }
    )
    .catch(this.handleErrors);
  }

The Import is this:

import { Http, Headers, Response } from "@angular/http";

I already red this issue, didn’t help => https://github.com/NativeScript/nativescript-angular/issues/927

I tried it with IP address which points to my localhost (same application as the yourwebsite.com) and that worked.


#2

Use fetch, instead of http.

There has been some issues in the past with using http, as in:



#3

@pentool thank you for your quick response; but the http code above is 1:1 from the tutorial of the website, why shouldn’t that work and why don’t they update the official tutorial when it doesn’t work?

I’ll try and give a feedback! Thank you again!


#4

but the http code above is 1:1 from the tutorial of the website

And so? There are many coding books published in the past (and present) that you pay for, yet, contain sample code that doesn’t work. Why is that?

and why don’t they update the official tutorial when it doesn’t work?

When I started out with {N}, I asked the same question about 200 times. Today, I just got used to it that not everything might be accurate and not everything is updated right away.

If you search the web for “nativescript http” you’ll find that for some http works, and for some it doesn’t. There are some who said http works for them on an emulator but fails on a real device. I don’t know why. All I can say is that I got frustrated with http, and when I tried fetch, it just worked for me out of the box without any issues.

You are welcome to play around with http of course. If you can get it working, that’s great.


#5

Uh, what? fetch uses http…

@CHOIX
Check whether the device is connected to the internet.


#6

Use this fix to get http working:

tns platform add android@3.4.0-2017-10-31-1