DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Identifying, Exploiting, and Preventing Host Header Attacks on Web Servers
  • How to Verify Domain Ownership: A Technical Deep Dive
  • Pragmatic Paths to On-Device AI on Android with ML Kit
  • Deep Linking in Enterprise Android Apps: A Real-World, Scalable Approach

Trending

  • Putting Guardrails Around What Your AI Agent Is Allowed to Touch
  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices
  • Multi-Agent Orchestration on AWS With AgentCore Runtime and A2A
  • Agentic Systems and Design Patterns

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Accessing Local Name-Based Virtual Hosts From the Android Emulator

Accessing Local Name-Based Virtual Hosts From the Android Emulator

By 
Dan Dyer
·
Jan. 23, 12 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

To test mobile versions of websites, it is useful to be able to connect to a web server on your local machine from a web browser on an Android emulator without having to expose the web server to the Internet.  You can’t use the normal loop-back IP address of 127.0.0.1 because that refers to the emulated Android device itself.  Instead you have to use 10.0.2.2 to connect to the host machine.

That’s fine if your local web server is serving a single site, but if you are using name-based virtual hosting to serve different sites depending on the host name of the request (with aliases for localhost defined in your machine’s hosts file), then you need to be making requests from the browser using the correct host name, not the IP address.

The Android emulator does not use the host machine’s hosts file for name resolution so attempting to access http://myvirtualhost in the emulator’s browser will not work.  This is because the emulated Android device has it’s own hosts file, so you have to update this to map the virtual host names to the local machine.

The first step is to start the AVD with an increased partition size otherwise you may get an out of memory error when you try to save the modified hosts file:

  emulator -avd MyAVD -partition-size 128

You then have to remount the system partition so that it is writeable:

  adb remount

Then copy the hosts file from the emulated device to the host machine:

  adb pull /etc/hosts

Edit the hosts file so that it includes mappings for all relevant virtual host names:

127.0.0.1        localhost
10.0.2.2         myvirtualhost1 myvirtualhost2

Then copy the updated file back to the emulated device:

  adb push hosts /etc/hosts

You should then be able to visit http://myvirtualhost1 in the emulator’s browser and see the correct site.

 

From http://blog.uncommons.org/2012/01/12/accessing-local-name-based-virtual-hosts-from-the-android-emulator/

Host (Unix) Android (robot) Web server

Opinions expressed by DZone contributors are their own.

Related

  • Identifying, Exploiting, and Preventing Host Header Attacks on Web Servers
  • How to Verify Domain Ownership: A Technical Deep Dive
  • Pragmatic Paths to On-Device AI on Android with ML Kit
  • Deep Linking in Enterprise Android Apps: A Real-World, Scalable Approach

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook