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 "127.0.0.1" 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 10.0.2.2 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.

127.0.0.1       localhost
10.0.2.2    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.

#!/bin/bash
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.

Running Spring Boot Web App on the Random Port from Port Range

By default the spring boot web application is listening on the port 8080 for the incoming connection.

This behavior can be changed by providing server.port property value during starting of the application or as part of the application.properties or through the code by implementing EmbeddedServletContainerCustomizer.

But it would be even better if we could specified a range of the ports which can be used for the starting the application.

It would be great if I could specify a property like server.portRange=8100..8200 to define a list of the port on which I want to start my service.

In this blog post I will describe how this can be done.