<p>在学Web的时候,可以直接通过localhost访问到本地服务器。所以在接触android网络编程中,依然在Socket中输入的localhost,结果确连接不上。</p><p>Log错误信息</p>
07-07 03:42:09.848: E/Trace(731): error opening trace file: No such file or directory (2)
07-07 03:42:10.409: I/tag(731): 线程开始
07-07 03:42:10.419: I/tag(731): 开始连接
07-07 03:42:10.498: W/System.err(731): java.net.ConnectException: failed to connect to /127.0.0.1 (port 8080) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)
07-07 03:42:10.598: W/System.err(731): at libcore.io.IoBridge.isConnected(IoBridge.java:224)
07-07 03:42:10.598: W/System.err(731): at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
07-07 03:42:10.598: W/System.err(731): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-07 03:42:10.618: W/System.err(731): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-07 03:42:10.618: W/System.err(731): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-07 03:42:10.628: W/System.err(731): at java.net.Socket.connect(Socket.java:842)
07-07 03:42:10.628: W/System.err(731): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
07-07 03:42:10.628: W/System.err(731): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-07 03:42:10.638: W/System.err(731): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
07-07 03:42:10.638: W/System.err(731): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-07 03:42:10.638: W/System.err(731): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-07 03:42:10.648: W/System.err(731): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
07-07 03:42:10.658: W/System.err(731): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
07-07 03:42:10.658: W/System.err(731): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
07-07 03:42:10.668: W/System.err(731): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
07-07 03:42:10.668: W/System.err(731): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
07-07 03:42:10.668: W/System.err(731): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
07-07 03:42:10.688: I/Choreographer(731): Skipped 52 frames! The application may be doing too much work on its main thread.
07-07 03:42:10.688: W/System.err(731): at com.Mrzhangxi.newsclient.MainActivity$1.run(MainActivity.java:43)
07-07 03:42:10.688: W/System.err(731): Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
07-07 03:42:10.698: W/System.err(731): at libcore.io.IoBridge.isConnected(IoBridge.java:208)
07-07 03:42:10.708: W/System.err(731): ... 17 more
07-07 03:42:10.728: D/gralloc_goldfish(731): Emulator without GPU emulation detected.
实际上localhost是指安卓自己本身了,并不是本地计算机的服务器。通过上网查询后发现,在模拟器上可以用10.0.2.2代替127.0.0.1和localhost。处了本地地址,其他地址不受影响