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.
- 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.
- After installation, start the tomcat server from the command line.
- 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.
- 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.
- 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.
- Open a terminal or dos window, and go to the platform-tools directory under the above android SDK location directory.
- 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
- Now you can open the web browser and browse web pages hosted in the android emulator web server from your PC.