Android模擬器(simulator)把它自己作為了localhost,也就是說,代碼中使用localhost或者127.0.0.1來訪問,都是訪問模擬器自己!這是不行的!
如果你想在模擬器simulator上面訪問你的電腦,那么就使用android內(nèi)置的IP 10.0.2.2 吧,10.0.2.2 是模擬器設(shè)置的特定ip,是你的電腦的別名alias
記住,在模擬器上用10.0.2.2訪問你的電腦本機。
詳細(xì)請參考Android文檔android-sdk-windows\docs\guide\developing\devices\emulator.html下的Emulator Networking
Network Address |
Description |
10.0.2.1 |
Router/gateway address |
10.0.2.2 |
Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine) |
10.0.2.3 |
First DNS server |
10.0.2.4 / 10.0.2.5 / 10.0.2.6 |
Optional second, third and fourth DNS server (if any) |
10.0.2.15 |
The emulated device's own network/ethernet interface |
127.0.0.1 |
The emulated device's own loopback interface |