Search code examples
cordovahttpandroid-emulatorcordova-pluginshttp-get

cordova-HTTP: most simple GET request not working


I'm trying to send a GET request from my Android emulator to the laptop on which the emulator is running, using the cordova plugin "cordova-HTTP":

cordovaHTTP.get(
            "http://10.0.2.2:80/",
            {},
            {},
            function(response) {
                console.log('success called');
            },
            function(error_response) {console.log('error called'); console.log(error_response.status);}
        );

The error callback gets called and the status code is 500 (= internal server error). But on my laptop I can see with tcpdump that no packages are even received. Why does this very simple GET request not work?

When I use Google Chrome on the emulator and navigate to "http://10.0.2.2:80/" everything works fine, I see the dummy page of the Apache2 webserver which is installed on my laptop.


Solution

  • Since API level 28 it is not allowed to send simple GET requests by default, because clear text traffic is not allowed. So I added the following to the config.xml of my cordova project (inside the <platform name="android"> tag):

    <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application"> 
    <application android:usesCleartextTraffic="true" /> 
    </edit-config>
    

    Also make sure to add the android XML namespace inside your <widget> tag, otherwise the above won't work:

    xmlns:android="http://schemas.android.com/apk/res/android"