我正在使用NativeScript构建应用程序,因此我的Android手机通过USB线连接到计算机进行调试。
但是当我在运行应用程序后尝试从手机发布时,它会一直给我这个错误:
System.err: java.net.ConnectException: Connection timed out
我尝试了所有,到目前为止,我不知道这是什么问题。
***当我尝试从“邮差”发布到服务器时,它运行正常。
谢谢你!import { Injectable } from "@angular/core";
import { User } from "../viewModels/userViewModel/userViewModel";
import { HttpClient } from '@angular/common/http';
@Injectable()
export class UserService {
private serverUrl= "http://192.168.14.155:61986/api/Users/PostUser";
constructor(private http: HttpClient) {}
register(user:User){
console.log("you are joing to use "+user.email+" as your Email");
const req = this.http.post(this.serverUrl, {
username: user.email,
email: user.email,
password: user.password
}).subscribe(
res => {
console.log(res);
},
err => {
console.log("Error occured");
}
);
}
}
这些是错误:
System.err: java.net.ConnectException: Connection timed out
System.err: at java.net.PlainSocketImpl.socketConnect(Native Method)
System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
System.err: at java.net.Socket.connect(Socket.java:586)
System.err: at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
System.err: at com.android.okhttp.Connection.connectSocket(Connection.java:1432)
System.err: at com.android.okhttp.Connection.connect(Connection.java:1390)
System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1667)
System.err: at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:133)
System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:466)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:503)
System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:130)
System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:261)
System.err: at org.nativescript.widgets.Async$Http$RequestOptions.writeContent(Async.java:313)
System.err: at org.nativescript.widgets.Async$Http$HttpRequestTask.doInBackground(Async.java:536)
System.err: at org.nativescript.widgets.Async$Http$1.run(Async.java:482)
System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
System.err: at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err: at java.lang.Thread.run(Thread.java:762)
JS: Error occured
答案 0 :(得分:1)
由于您的手机和PC位于不同的网络中,请通过USB连接您的设备并在命令提示符中输入以下命令:
adb reverse tcp:61986 tcp:61986
然后使用而不是http://192.168.14.155:61986/api/Users/PostUser
http://localhost:61986/api/Users/PostUser