Many times when developing Android applications, you’re doing it to pair with a web server. You’re planning on creating this awesome web service / Android experience, but you aren’t quite ready to publish the web service to the internet.
The following will show you how to connect the Genymotion Android emulator to a locally hosted web application.
If you’ve ever worked with a simulator before, you know that the IP may not always match the network of your host machine. For example, I use an Apple Airport Extreme wireless router at home and it broadcasts with the subnet 10.0.1.x. This is pretty non-standard in comparison to networks that might broadcast on 192.168.0.x.
This tutorial assumes two things:
There are a few ways you can mock up a localhost server for this tutorial. If you’re on Mac or Linux like I am, you can create a directory with a website in it and create an HTTP server with Python. Launching it could look something like this:
python -m SimpleHTTPServer
That would give you a web application running at http://localhost:8000. If you’re interested in Node.js, you could take a look at my previous tutorial regarding Express.js. That would give you a web application running at http://localhost:3000.
In any case, boot up your Genymotion simulator. When it has booted, from your host machine run the following from the Terminal:
ifconfig vboxnet0
The above command is for Mac and Linux, but if you’re on Windows, just replace ifconfig
with ipconfig
.
You should end up with a result similar to this:
vboxnet0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:00
inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255
The inet
value is going to represent the IP of the host machine from within the Genymotion simulator.
Assuming you’ve chosen to use Python to run your web application, enter http://192.168.56.1:8000 in your Genymotion web browser. Remember the actual IP could be different depending on what the previous command returned.
The same address can be used within your application code.
A video version of this article can be seen below.