Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

android - Retrofit cannot connect to my local web server on port 3000

My app that is using Retrofit (v1.6.1) to connect to REST web services cannot connect to my local web server (running on localhost), this is the error I am getting.

This is inside Nexus4 (v4.3) emulator, I am using Android Studio for development and I have added android.permission.INTERNET & android.permission.ACCESS_NETWORK_STATE permissions in manifest file.

The error says its waited for 15s but it does not, it errors out right away.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On your phone connecting to localhost:3000 means "connect to port 3000 on local network interface" - which is local interface of the phone, not the port 3000 of the PC to which your phone is connected. There is nothing listening on port 3000 on the phone, so you get connection refused error.

Whould you're trying to do is (probably, hard to guess without more information) is to connect from the phone to port 3000 on you PC. So you should change the address you're connecting to from localhost to address of the PC. In case of emulator that would be 10.0.2.2 (which is localhost of the PC). See networking information for reference.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...