Android Emulator and local web server (Denwer)

    imageI will continue to share some of the development cases for Android and now I want to talk about how to simply make the application running in the emulator work with your server API, which lies on the local web server. In simple words - how to communicate the application from the emulator via http with localhost. These are not professional advice, but nothing 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 drawings on my phone to an online gallery. In my case, the gallery is its own site, which receives a picture and some text variables in a POST request. Denwer running on a windows machine to emulate a web server, access to the gallery project by http is organized by url _http: //dev.gallery.
    And so, the problem is that the emulator does not see your own hosts with host descriptions and, accordingly, does not know about _http: //dev.gallery. Attempts to place the project in localhost / dev.gallery also lead to nothing, because 127.0.0.1 for the emulator is it himself. The problem is solved by making changes to the emulator hosts. Specifically, strings 10.0.2.2 dev.gallery. This address must 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:
    • Run the emulator with your hands emulator -avd ИмяУстройства -partition-size 128
    • In the new console window, execute adb remount
    • Copy hosts from the device to the root of the disk adb pull /system/etc/hosts c:\hosts
    • We open the received 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 the line 127.0.0.1 localhost(most likely it will be the only data line in the file) to 10.0.2.2 localhost, but it will not be kosher.
    • Spread our hosts on the 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 that launches the emulator with the necessary parameters and the second, replacing hosts with pre-prepared if necessary.

    All this was used when debugging the operation of the PaintUp application and the PaintUp.net gallery

    Also popular now: