gpt4 book ai didi

android - 改造无法连接到端口 3000 上的本地 Web 服务器

转载 作者:IT老高 更新时间:2023-10-28 22:12:06 26 4
gpt4 key购买 nike

我使用 Retrofit (v1.6.1) 连接到 REST Web 服务的应用程序无法连接到我的本地 Web 服务器(在 localhost 上运行),这是我遇到的错误。

这是在 Nexus4 (v4.3) 模拟器中,我使用 Android Studio 进行开发,我添加了 android.permission.INTERNETandroid.permission.ACCESS_NETWORK_STATE list 文件中的权限。

错误说它等待了 15 秒,但它没有,它立即出错。

07-13 15:57:53.947      820-840/it.test.android.app.activity D/Retrofit﹕ ---> HTTP POST http://localhost:3000/app/auth/signin
07-13 15:57:54.132 820-840/it.test.android.app.activity D/Retrofit﹕ Content-Type: application/json; charset=UTF-8
07-13 15:57:54.132 820-840/it.test.android.app.activity D/Retrofit﹕ Content-Length: 44
07-13 15:57:54.157 820-840/it.test.android.app.activity D/Retrofit﹕ {"auth":{"email":"steve","password":"pass"}}
07-13 15:57:54.387 820-840/it.test.android.app.activity D/Retrofit﹕ ---> END HTTP (44-byte body)
07-13 15:57:54.657 820-840/it.test.android.app.activity D/Retrofit﹕ ---- ERROR http://localhost:3000/app/auth/signin
07-13 15:57:54.857 820-840/it.test.android.app.activity D/Retrofit﹕ java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3000) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:223)
at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:68)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:208)
            at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
            at libcore.io.IoBridge.connect(IoBridge.java:112)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
            at java.net.Socket.connect(Socket.java:842)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
            at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
            at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
            at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
            at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
            at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
            at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
            at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
            at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
            at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
            at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:68)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
            at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
            at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
            at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at retrofit.Platform$Android$2$1.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:841)
07-13 15:57:54.857 820-840/it.test.android.app.activity D/Retrofit﹕ ---- END ERROR

最佳答案

在您的手机上连接到 localhost:3000 表示“连接到本地网络接口(interface)上的端口 3000” - 这是手机的本地接口(interface),而不是您手机所在 PC 的端口 3000连接的。手机的 3000 端口没有监听,所以出现连接被拒绝错误。

您想要做的是(可能,如果没有更多信息,很难猜到)是从手机连接到您 PC 上的端口 3000。因此,您应该将要连接的地址从 localhost 更改为 PC 的地址。如果模拟器是 10.0.2.2 (这是 PC 的本地主机)。 See networking information供引用。

关于android - 改造无法连接到端口 3000 上的本地 Web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24726670/

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