我正在使用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

最佳答案

由于您的手机和PC位于不同的网络中,因此请通过USB连接设备,然后在命令提示符下输入以下命令:

adb reverse tcp:61986 tcp:61986

然后代替http://192.168.14.155:61986/api/Users/PostUser使用
http://localhost:61986/api/Users/PostUser

关于android - 我试图发布到本地主机API,我得到-> java.net.ConnectException:连接超时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50731503/

10-09 12:39