
I will continue to share some development cases for Android and now I want to talk about how easy it is to make an application running in the emulator work with your own server API, which lies on the local web server. In simple words - how to communicate to the application from the emulator via http with localhost. This is not professional advice, and no more than an example of a specific working solution.
I'll start with the conditions of the problem. My application, which I
described last time , needed the ability to publish pictures on the phone in an online gallery. In my case, the gallery is its own website, which in a POST request receives an image and some text variables.
Denwer is running on a windows-machine to emulate a web server, access to the gallery project via http is organized by urlu _http: //dev.gallery.
And so, the problem is that the emulator does not see your own hosts with host descriptions and, therefore, does not know about _Http: //dev.gallery. Attempts to place the project in localhost / dev.gallery also do not lead to anything, since 127.0.0.1 for the emulator is himself. The problem is solved by making changes to the emulator hosts. Specifically - lines
10.0.2.2 dev.gallery
. This address should be used if you want to access the services hosted on your machine. A complete list of address space addresses is
here .
In order to make the appropriate changes:
- We start the emulator with the command
emulator -avd -partition-size 128
- In the new console window, perform
adb remount
- Copy the hosts from the device to the root of the
adb pull /system/etc/hosts c:\hosts
disk adb pull /system/etc/hosts c:\hosts
- Open the file with a text editor and make the necessary changes. By the way, if you need to give access to the entire localhost folder, you can change the address in
127.0.0.1 localhost
line (most likely it will be the only data line in the file) to 10.0.2.2 localhost
, but it will not be kosher. - We post our hosts on the
adb push c:\hosts /system/etc
device adb push c:\hosts /system/etc
After that you can safely knock on your web server. If you restart the emulator, the hosts will have to be replaced again. To speed up the work, it is convenient to create and use one cmd-file, which starts the emulator with the necessary parameters and the second one, replacing the hosts with the previously prepared ones if necessary.
All this was used to debug the work of the
PaintUp application and the
PaintUp.net gallery
.