开发者

Unable to contact host machine (10.0.2.2) from emulator

I have a python REST API server running on my laptop. I am trying to write a rest client in Android (using Eclipse ADT etc) to contact it using Apache's client (org.apache.http.client) libraries.

The code is really simple, and basically does the following -

HttpGet httpget = new HttpGet(new URI("http://10.0.2.2:8000/user?username=tim"));
HttpResponse response = httpclient.execute(httpget);

However at execute, it exceptions out with a time out exception. I cannot hit the URL even from the browser in the emulator.

Details of the exception

org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.2.2:8000 timed out

However, I tried using the cREST client on Chrome on my laptop, and I am a开发者_JS百科ble to query the REST server fine.


Is it possible the machine is not on your network? Ie - it is on the other side of a router, on the internet somewhere? Because addresses starting "10." are reserved as private addresses and not routable.

See http://en.wikipedia.org/wiki/Private_network for more info


I had the same issue and here's how I figured out the cause:

Quit you emulator. Start your local server. In the browser, you should be able to access "http://localhost:8000/user?username=tim" and get some response. If you get a timeout, your server is likely not running. In my case, my python server had a break-point set and it was stuck there. Once I let it run, I was able to see responses on the browser and subsequently in the emulator (using 10.0.2.2).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜