首页 > 代码库 > java.net.ConnectException: failed to connect to /127.0.1.1 (port 8080)的解决办法

java.net.ConnectException: failed to connect to /127.0.1.1 (port 8080)的解决办法


当你启动Tomcat后,在浏览器中输入http://127.0.1.1:8080/xxx.xx,你会发现你可以找打你要找的东西。但是当你在Eclipse中运行测试代码后,JUnit会一直报出这样的错误:


java.net.ConnectException: failed to connect to /127.0.1.1 (port 8080) after 5000ms: 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:843)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
at com.android.okhttp.Connection.connect(Connection.java:101)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)


此时,你的第一反应可能是某出代码有问题或者是清单文件中某个权限忘加了,其实不是这样的。原来模拟器默认把127.0.0.1和localhost当做本身了,在模拟器上可以用10.0.2.2代替127.0.0.1和localhost,另外如果是在局域网环境可以用 192.168.0.x或者192.168.1.x(根据具体配置)连接本机,这样应该就不会报错了。

本文出自 “江城” 博客,请务必保留此出处http://523436424.blog.51cto.com/8575872/1548436

java.net.ConnectException: failed to connect to /127.0.1.1 (port 8080)的解决办法