开发者

Android "localhost" sockets fail when no data connection?

I'm desi开发者_JAVA百科gning an Android app that will require the use of a web server on the local device. I've been trying out some different servers for this purpose. At present I haven't written any code or run anything in an emulator, just played around with the servers on my actual phone, and I'm observing some strange behaviour.

Whenever I try to connect to the local web server, and I have WIFI switched off, the HTTP request fails. If I switch WIFI on, it succeeds. Depending on which address I use, I get different results: using localhost or 127.0.0.1, I get connection refused when WIFI is off; using the current 10.X.Y.Z address I get a timeout. Both addresses work when WIFI is on.

I have tried this with xWS, PAW and i-Jetty: the behaviour is consistent. WIFI on, I can connect to the local web server; WIFI off, I can't. I am using the default "Internet" browser on Android 2.3.3 on a Samsung Galaxy S (GT-I9000).

Does anyone know why this is? Is it a simple question of a setting somewhere I need to change, or what's going on?

Cheers,

/Uffe


Can you change the network interface the web server is connected to? The default setting is probably something like eth0 or wlan0, if you change it to lo it could work.


You are asking why when you turn off your wifi, there is no connection to the 10.0.0.2 (Your computer's localhost) and when you turn it on, there is a connection?

Very simple. Wifi off - NO INTERNET connection. The device emulator is a separate OS inside your computer's OS. When you turn off your WIFI there is no route to the host (your computer) - there is no internet


I have now tested with my own simple client, and with Opera - and it works.

So in fact this is an issue not with the IP stack but with the default Android browser, or possibly with the settings enforced by the manufacturer (Samsung) or carrier (Telenor Sweden). Either way there is a workaround: use Opera instead.

Still don't know why it doesn't work with the default browser, but I'll mark this question answered.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜