When Android reads a file from tomcat, the following exception occurs:
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. <init> (httpconnection. Java: 76)
08-10 14:53:09. 158: W/system. Err (12527): At libcore.net. http. httpconnection. <init> (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. low.loop (low.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
Solution:
String url = "http: // localhost: 8080/notice/test.txt"; modify it to string url = "http: // 192.168.1.100: 8080/notice/test.txt ";
The Host IP address cannot use localhost or 127.0.0.1. Use the real IP address.