Sometimes you need connect to local web server running in your PC from android emulator. And sometimes you need use PC web browser to browse web pages stored in web server running in android emulator. These two scenario are commonly use case when you develop android application. This article will tell you how to implement.
1. Connect To Web Server Running On PC From Android Emulator.
- Install a web server such as tomcat in your PC. You can read article How To Install Tomcat 9.0 Correctly On Your Machine and How To Install Tomcat In MacOS.
- After installation start tomcat server from command line.
- Open a web browser in android emulator and input http://10.0.2.2:8089 in url address text box. 10.0.2.2 is an alias ip to access the PC where android emulator execute in. 8089 is the tomcat server listening port number.
2. Connect Web Server Running On Android Emulator From Local PC.
- Suppose the web server listening on port number 8090 in android emulator, and you want to access that web server from the emulator hosted PC web browser, you should do followings.
- Get android SDK location. You can open android studio, click File —> Project Structure menu item in the top tool bar. Then you can find the android SDK location in the popup dialog.
- Open a terminal or dos window, and go to platform-tools directory under above android SDK location directory. Then run below command to forward http tcp request to local PC port 8090 to android emulator port 8090.
adb forward tcp:8090 tcp:8090
- Now you can open web browser and browse web pages hosted in android emulator web server from your PC.