gpt4 book ai didi

android - IP 地址的 Nativescript http.post() MalformedURLException

转载 作者:行者123 更新时间:2023-11-30 00:27:26 27 4
gpt4 key购买 nike

问题:http.post() 返回

Error: java.net.MalformedURLException: Protocol not found: 192.168.1.14:8080/newuser at ZoneAwareError

代码(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);
}
}

完成杂货教程(第 3 章:服务)后,我无法访问用户的后端 api,因此我使用 NodeJS 编写了自己的 API(托管在我网络上的 OPi 上)。我可以使用我的 dev comp 发布,并且我可以从模拟器的浏览器访问 GET 请求,但是当我尝试发布 IP:port 时,我得到一个 MalformedURLException .

我如何发布到这个 URL?我能找到的所有 Nativescript http.post() 示例都使用基于 DNS 的 URL;甚至可以进行基于 IP 的请求吗?

最佳答案

是的,可以使用基于 IP 的请求。但是,您需要提供 http:// 前缀,并且在通过模拟器工作时,您应该考虑环回地址与您的开发机器上的地址不同

当使用 Android AVD 时,环回地址是 10.0.2.2(对于 GenyMotio,环回是不同的),而在 iOS 上,环回是 localhost。因此,如果您想使用本地 IP 地址,则需要考虑到这一点。

例如here

关于android - IP 地址的 Nativescript http.post() MalformedURLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45092739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com