How To Connect To A Http Server Running In Android Emulator From PC Vice Versa

Sometimes you need to connect to the local webserver running on your PC from the android emulator. And sometimes you need to use a PC web browser to browse web pages stored in a web server running in an android emulator. These two scenarios are common use cases when you develop android applications. This article will tell you how to implement it.

1. Connect To Web Server Running On PC From Android Emulator.

  1. Install a web server such as tomcat on your PC. You can read the article How To Install Tomcat 9.0 Correctly On Your Machine and How To Install Tomcat In MacOS.
  2. After installation, start the tomcat server from the command line.
  3. Open a web browser in the android emulator and input http://10.0.2.2:8089 in url address text box. The IP address 10.0.2.2 is an alias IP to access the PC where the android emulator executes. The port number 8089 is the tomcat server listening port number.

2. Connect Web Server Running On Android Emulator From Local PC.

  1. Suppose the webserver is listening on port number 8090 in the android emulator, and you want to access that webserver from the emulator-hosted PC web browser, you should do the following.
  2. Get Android SDK location. You can open android studio, click File —> Project Structure menu item in the top toolbar. Then you can find the android SDK location in the popup dialog.
  3. Open a terminal or dos window, and go to the platform-tools directory under the above android SDK location directory.
  4. Then run the below command to forward the http TCP request to local PC port 8090 to android emulator port 8090.
    adb forward tcp:8090 tcp:8090
  5. Now you can open the web browser and browse web pages hosted in the android emulator web server from your PC.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.