Skip to main content

Accessing "localhost" on host machine from android emulator

During developing mobile web application for android platform it would be nice that you are able to test it. For testing purpose, if you don't have android phone, you can use Android emulator which is included in Android SDK.
During testing you will need to access application hosted on your local machine, but typing of  "localhost" or "" will not lead you to your web server, on your developing machine (emulator host), instead you will get message that Page is not available. Accessing web application on your emulator host can be done by typing in web browser on your android emulator. But in some cases accessing by IP address is not preferred way, eg when you host more pages on the same server. 
Currently I am working on developing application which use OpenCMS. OpenCMS is able to host more sites in the same time, so in my case every customer have it's own web site on the same server. But direct access with IP address will lead me to see my JBoss, and i will not be able to see pages of some particular customer.
To resolve this problem you can use Android Debug Bridge.
First i started emulator and used pull command to get "hosts" file from android device.

./adb pull system/etc/hosts hosts

This will copy content of hosts file from emulator to your PC. After that I added one new line of code and copy changed file to emulator.       localhost    blau vam rot pvr

First time when I tried to copy file back to the emulator, I got the message that there is no more free space on emulator. Solution of this problem is starting device with more space the default, it can be done with next command:

./emulator -partition-size 128 @Android &

After that i was able to run push command without any problems, of course after mounting system with read and write permission.
In next few lines you can see simple script used for starting emulator with prepared "hosts" file.

echo "Starting Android 2.2 Emulator..."
./emulator -partition-size 128 @Android &
echo "Emulator started.\n Configuring device."
../platform-tools/adb wait-for-device remount
../platform-tools/adb wait-for-device push ../platform-tools/hosts system/etc/hosts
echo "Device configured."

Popular posts from this blog

Checking file's "magic numbers"

Few days ago I had very interesting task. Our customer required that we perform checking of so called file's "magic numbers" to determinate does uploaded file correspond to it's extension. 
We are already allowed only to upload files with some predefined extensions (PDF, DOC ...). But this can not prevent some evil user to update an exe file after renaming it to PDF or DOC.
So first of all I will explain what are "magic numbers", and then I will show how we handle them.

Simple Workflow Engine With Spring

Few months ago, during working on one of the company project, we had need to developed  REST services which is used for sending an email depending on data sent by client application. During developing this service we decide to create simple workflow engine which will be charged for sending an email, but also this engine can be used for any kind of simple flows. In this article i will explain step by step how you can implement your simple workflow engine which can handle sequence flow.

Configure Seam Mail

From time to time we have need to send an e-mail from application. This task should not be so complicated, but if you do it for the first time you can get some troubles and it can take you some time. Because of that I will try to explain how you can configure it correctly.