我在尝试实现找到here
的Nativescript教程代码时遇到以下错误 "originalStack": "Error: java.net.ConnectException: Connection refused\n
at ZoneAwareError (file:///data/data/org.nativescript.NotecardRestaurants/
files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:993:33)\n...
以下是app.module的来源
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { AppRoutingModule } from "./app.routing";
import { AppComponent } from "./app.component";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { NativeScriptHttpModule } from "nativescript-angular/http";
import { UserService } from './shared/user/user.service';
import { LoginComponent } from "./login/login.component";
@NgModule({
bootstrap: [
AppComponent
],
imports: [
NativeScriptModule,
AppRoutingModule,
NativeScriptFormsModule,
NativeScriptHttpModule,
],
declarations: [
AppComponent,
LoginComponent
],
providers: [
UserService
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class AppModule { }
以下是导致问题的user.service的来源
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(
Config.apiUrl + "/user/register",
JSON.stringify({
name: user.name,
email: user.email,
password: user.password
}),
{ headers: headers }
)
.map(response => response.json().token)
.catch(this.handleErrors);
}
login(user: User) {
let headers = new Headers();
headers.append("Content-Type", "application/json");
return this.http.post(
Config.apiUrl + "user/login",
JSON.stringify({
email: user.email,
password: user.password
}),
{ headers: headers }
)
.map(response => response.json().token)
.catch(this.handleErrors);
}
handleErrors(error: Response) {
console.dir(error.json());
return Observable.throw(error);
}
}
我已经独立验证了我正在使用的api的完整性。给出的路线确实有效。我正在使用
运行nativescript项目 tns run android --emulator
答案 0 :(得分:0)
如果您在IOS模拟器或Android模拟器(基本上是虚拟机)上运行应用程序,并且如果引用的是“ localhost”或“ 127.0.0.1”,则这些是虚拟机的IP,而不是您的开发计算机。您可以通过提供开发机的IP来解决问题,或者在android中,您可以将10.0.2.2 IP用于开发机。您的api网址示例是 https://10.0.2.2:5001/api/news/getnews