Reputation: 115
Problem: http.post()
returns
Error: java.net.MalformedURLException: Protocol not found: 192.168.1.14:8080/newuser at ZoneAwareError
Code (user.service.ts):
import { Injectable } from "@angular/core";
import { Http, Headers, Response } from "@angular/http";
import { Observable } from "rxjs/Rx";
import "rxjs/add/operator/do";
import "rxjs/add/operator/map";
import { User } from "./user";
import { Config } from "../config";
@Injectable()
export class UserService {
constructor(private http: Http) {}
register(user: User) {
let headers = new Headers();
headers.append("Content-Type", "application/json");
return this.http.post(
"192.168.1.14:8080/newuser",
JSON.stringify({
username: user.email,
password: user.password
}),
{ headers: headers }
)
.catch(this.handleErrors);
}
handleErrors(error: Response) {
console.log(JSON.stringify(error.json()));
return Observable.throw(error);
}
}
Following through the groceries tutorial (Chapter 3: Services), I couldn't access the backend api for the users so I wrote my own using NodeJS
(hosting on an OPi on my network). I can post using my dev comp, and I can access a GET
request from the browser of the emulator, but when I try to post the IP:port I get a MalformedURLException
.
How can I post to this URL? All of the Nativescript http.post()
examples I could find use DNS-based URLs; is it even possible to do IP-based requests?
Upvotes: 1
Views: 463
Reputation: 9670
Yes, it is possible to use IP-based requests.
However, you will need to provide the http://
prefix and also when working through emulator you should consider that the loopback address is not the same as on your development machine
When using Android AVDs the loopback address is 10.0.2.2
(for GenyMotio the loopback is different) while on iOS the loopback is localhost
. So if you want to use local IP addresses you will need to take that into consideration.
e.g. here
Upvotes: 1