This article attempts to address the question of how to access a local Web application on the Android emulator. For example, start a Tomcat service on your development machine, then open the browser on your computer, and by default you can access Tomcat's home page by typing http://localhost:8080/, so if we want to access the Android emulator, can we? The answer is yes, but the URL of the visit will change. Please continue to look down.
In general Java Web Application development, we typically use localhost or 127.0 0.1来 to access native Web services, but if we use the same address in the Android emulator, the Android emulator will not be able to access our services properly. What is this for? We can understand that: the bottom of the aatchndroid is the Linux kernel, including Android itself is an operating system, so we entered in the emulator browser localhost or 127.0.0.1 generation It's the Android simulator (Android virtual machine), not your computer, okay? This is why you will report "Web page not available" When you use localhost in the emulator.
So how do you get access to Web apps on your local computer? In Android, the address of our local computer is mapped to 10.0.2.2, so you can access the Web resources on the local computer on the emulator simply by replacing the original localhost or 127.0.0.1 with 10.0.2.2.
So let's start Tomcat on the computer, then run the Android emulator, open the browser on the emulator, and then visit: http://10.0.2.2:8888 have you seen that cute tomcat cat? The access screenshot is shown in the following figure. (Note: On my computer, the Tomcat service uses a port of 8888, the default is 8080)