(轉載自: http://www.80x86.cn/article.asp?id=1843)

調試中通過android simulator模擬器鏈接localhost或者127.0.0.1,因為我在電腦上面建立了apache,我的代碼大概就是
URL url = new URL(urlString); 
    URLConnection urlconn = url.openConnection();

但是報錯了! 

Exception 1:java.net.ConnectException: localhost/127.0.0.1:8080 - 
Connection refused

問題是這樣的,android模擬器(simulator)把它自己作為了localhost,也就是說,代碼中使用localhost或者127.0.0.1來訪問,都是訪問模擬器自己! 這是不行的!

如果你想在模擬器simulator上面訪問你的電腦,那麼就使用android內置的IP 10.0.2.2 吧, 10.0.2.2是模擬器設置的特定ip,是你的電腦的別名alias

記住,在模擬器上用10.0.2.2訪問你的電腦本機