在开发与Internet交互的应用中,往往在本机用apache或IIS搭建了一个测试站点,往往通过localhost和127.0.0.1来访问。但将地址丢到android里面,就会冒出下面的错误
11-25 06:07:33.126: WARN/System.err(267): org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused
11-25 06:07:33.136: WARN/System.err(267): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
11-25 06:07:33.136: WARN/System.err(267): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-25 06:07:33.136: WARN/System.err(267): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-25 06:07:33.136: WARN/System.err(267): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
11-25 06:07:33.136: WARN/System.err(267): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-25 06:07:33.136: WARN/System.err(267): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-25 06:07:33.136: WARN/System.err(267): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-25 06:07:33.147: WARN/System.err(267): at me.atu.findCars.ui.LoginActivity$1.onClick(LoginActivity.java:78)
11-25 06:07:33.147: WARN/System.err(267): at android.view.View.performClick(View.java:2408)
11-25 06:07:33.147: WARN/System.err(267): at android.view.View$PerformClick.run(View.java:8816)
11-25 06:07:33.147: WARN/System.err(267): at android.os.Handler.handleCallback(Handler.java:587)
11-25 06:07:33.147: WARN/System.err(267): at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 06:07:33.147: WARN/System.err(267): at android.os.Looper.loop(Looper.java:123)
11-25 06:07:33.147: WARN/System.err(267): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-25 06:07:33.147: WARN/System.err(267): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 06:07:33.147: WARN/System.err(267): at java.lang.reflect.Method.invoke(Method.java:521)
11-25 06:07:33.147: WARN/System.err(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-25 06:07:33.147: WARN/System.err(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-25 06:07:33.147: WARN/System.err(267): at dalvik.system.NativeStart.main(Native Method)
11-25 06:07:33.156: WARN/System.err(267): Caused by: java.net.ConnectException: /127.0.0.1:80 - Connection refused
11-25 06:07:33.156: WARN/System.err(267): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:254)
11-25 06:07:33.156: WARN/System.err(267): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
11-25 06:07:33.156: WARN/System.err(267): at java.net.Socket.connect(Socket.java:1055)
11-25 06:07:33.156: WARN/System.err(267): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-25 06:07:33.156: WARN/System.err(267): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
11-25 06:07:33.167: WARN/System.err(267): ... 18 more
关键是这一行:org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused
大意是连接127.0.0.1被拒绝了。把地址换成了localhost也不行,让我真是一团雾水,找了些资料才搞明白,原来启动的android模拟器吧自己也当成127.0.0.1和localhost
解决办法:用本机的内网地址,如192.168.0.5这样的就可以正常调试了