android 从tomcat读取文件出错:connect failed: ECONNREFUSED

android 从tomcat读取文件时出现以下异常:

08-10 14:53:09.118: W/System.err(12527): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused)
08-10 14:53:09.118: W/System.err(12527):  at libcore.io.IoBridge.connect(IoBridge.java:114)
08-10 14:53:09.128: W/System.err(12527):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-10 14:53:09.128: W/System.err(12527):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-10 14:53:09.128: W/System.err(12527):  at java.net.Socket.connect(Socket.java:842)
08-10 14:53:09.158: W/System.err(12527):  at libcore.net.http.HttpConnection.(HttpConnection.java:76)
08-10 14:53:09.158: W/System.err(12527):  at libcore.net.http.HttpConnection.(HttpConnection.java:50)
08-10 14:53:09.158: W/System.err(12527):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
08-10 14:53:09.168: W/System.err(12527):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-10 14:53:09.168: W/System.err(12527):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-10 14:53:09.178: W/System.err(12527):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
08-10 14:53:09.178: W/System.err(12527):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
08-10 14:53:09.178: W/System.err(12527):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
08-10 14:53:09.178: W/System.err(12527):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
08-10 14:53:09.208: W/System.err(12527):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
08-10 14:53:09.208: W/System.err(12527):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
08-10 14:53:09.208: W/System.err(12527):  at com.example.androidstudy.utils.HttpFileDownloadUtils.download(HttpFileDownloadUtils.java:31)
08-10 14:53:09.218: W/System.err(12527):  at com.example.androidstudy.FileDownloadActivity$1$1.handleMessage(FileDownloadActivity.java:36)
08-10 14:53:09.218: W/System.err(12527):  at android.os.Handler.dispatchMessage(Handler.java:95)
08-10 14:53:09.232: W/System.err(12527):  at android.os.Looper.loop(Looper.java:137)
08-10 14:53:09.232: W/System.err(12527):  at android.os.HandlerThread.run(HandlerThread.java:60)
08-10 14:53:09.238: W/System.err(12527): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
08-10 14:53:09.251: W/System.err(12527):  at libcore.io.Posix.connect(Native Method)
08-10 14:53:09.251: W/System.err(12527):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
08-10 14:53:09.251: W/System.err(12527):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-10 14:53:09.258: W/System.err(12527):  at libcore.io.IoBridge.connect(IoBridge.java:112)
08-10 14:53:09.258: W/System.err(12527):  ... 19 more

解决方法:

String url = "http://localhost:8080/notice/test.txt";  修改成 String url = "http://192.168.1.100:8080/notice/test.txt";

主机ip不能使用localhost或者127.0.0.1,使用真实ip地址即可。


 

你可能感兴趣的:(android)