Retrofit无法连接到端口3000上的本地Web服务器

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

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

错误说它等了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 

Solutions Collecting From Web of "Retrofit无法连接到端口3000上的本地Web服务器"

在连接到localhost:3000的手机上意味着“连接到本地networking接口上的端口3000” – 这是手机的本地接口,而不是手机所连接的PC的端口3000。 在电话上没有任何端口3000监听,所以你得到连接拒绝错误。

你试图做的是(可能很难猜到没有更多的信息)是从电话连接到您的PC上的端口3000。 所以你应该改变你连接的地址从localhost到PC的地址。 仿真器的情况下,将10.0.2.2 (这是PC的本地主机)。 查看networking信息以供参考。

我解决了这个问题,需要使用10.0.2.2而不是localhost来连接到我的本地服务器。

在这里find它 – http://developer.android.com/tools/devices/emulator.html#networkaddresses