My Android application talks to a php server. I have Apache/php running locally on WAMP Server. I am running Windows 7. If I go to localhost on Windows 7 the site shows but on the phone it will look for localhost on the current Android OS so no site shows.
I managed to get around this by opening my apache configuration to the internet and then using my external IP address, this is annoying because I am on a dynamic IP and sometimes don't have internet (+ not secure).
How do I make a http request from android access localhost (or another alias in the hosts file)?
You can access your host machine with the IP address "10.0. 2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".
i.e you can use technitium.com/dns) and open port 53; and add zone of shop. test with A record value point to IP of computer installed apache. Then change DNS IP of Android or iOS phone (guide to change can search on google). Then on the phone you can access the site.
If you've ever wanted to install a web server on your Android device, you're in luck. This walk-through explains how to install and configure HTTP Server powered by Apache. From the office of “We do this because we can” comes the ability to run a web server on an Android device.
Jon is correct that in the general case you cannot make the android device connect to a pc through the usb cable (a major difference from the android emulator where such connection is accomplished by an alias address). However, there are some things you can do:
You can connect both the pc and the phone to a secured wifi router. It doesn't even need to have an upstream internet connection.
Apparently, if you have froyo and the usb tethering option, turning this on will as a side effect establish networking over USB that allows the phone to contact the pc (just watch where you pc's internet traffic is going though)
If you wanted something on the pc to open a connection to something listening on the phone, you could use an adb port forward. But it sounds like you want a client on the pc to contact a server on the phone, so that won't work.
In general, you can't.
What you can do, if the computer is on the same wireless network as the Android device, is connect to the computer's internal IP. For example, on a typical home wifi router, the router is at 192.168.1.1
, and the devices connected to it are 192.168.1.x
. So if your Windows 7 PC is at 192.168.1.5
, you should be able to access this from your Android device just fine...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With