5

I'm trying to execute the following function on Android to sync with an IoT device I'm making:

function NewDeviceFetchDeviceID(){
    strURL = "https://192.168.x.x/.....";

    return $.ajax({
        url: strURL,
        type: 'GET',
        timeout: 90000
    });
}

The device takes some time to churn a response to the request (about 45-50 seconds) so I need the timeout to be slightly longer than 30s. The timeout seems to work on iOS - if I set it to 5 seconds it will do so, if I set it for 90 it will wait the whole time. For Android, it seems that this argument is ignored.

I tried adding the following into config.xml under <platform name="android"> with no luck:

<preference name="LoadUrlTimeoutValue" value="90000"/>

and I tried with a lowercase "L", still no luck:

<preference name="loadUrlTimeoutValue" value="90000"/>

How can I properly increase the timeout argument for AJAX requests in a Cordova Android app?


Update: I've changed the function, and I indeed get "Timed out!!!" as the response after exactly 30 seconds:

function TryXHR(){
    try{
        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                alert("ready state = 4");
            }
        };

        strURL = "https://192.168.x.x/......";
        xhr.open("POST", strURL, true);
        xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
        xhr.timeout = 120000; // Set timeout to 120 seconds (120000 milliseconds)
        xhr.onload  = function () { alert(xhr.response); }
        xhr.ontimeout = function () { alert("Timed out!!!"); }
        xhr.onerror = function () { alert("Non-timeout error"); }
        xhr.send();
    }catch(err){
        alert("exception caught: "+err.message);
    }
}
Lil' Bits
  • 898
  • 2
  • 9
  • 24
  • I think it is answered here https://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java – CarlosCarucce Sep 02 '19 at 16:10
  • Unfortunately that's in Java, and I'm writing my app with JavaScript in Cordova – Lil' Bits Sep 03 '19 at 20:04
  • Did you cross checked server too? There is something called as server timeout too. [Check this](https://www.php.net/manual/en/function.set-time-limit.php) Check in network tab if the request going have an timeout param. Hope that helps :) – Prajyot Tote Sep 04 '19 at 09:07
  • There is no timeout limit on the server side. I can see that server is still decrypting the incoming information from the mobile app, and the (Android) mobile app gives up. – Lil' Bits Sep 05 '19 at 03:25
  • instead to `return $.ajax({.....` do yuo have tried to use $.ajax `complete()` method to fire your callback? try to set an error function too, to be sure that timeout is the real problem with the $ajax `error()` method `error: function(x, t, m) {if(t==="timeout") { alert("got timeout"); } else { alert(t); } }` – Sim1-81 Sep 09 '19 at 14:41
  • @Sim1-81 see update in original post – Lil' Bits Sep 18 '19 at 02:31
  • Is your server behind a load balancer? – karthick Sep 18 '19 at 19:43
  • @karthick no, the device is just low-resource, hence why the response takes so long to come back – Lil' Bits Sep 18 '19 at 19:52
  • @Lil' Bits for curiosity, strURL = "https://192.168.x.x/......"; seems to be a local environment, are you sure that you can perform request under HTTPS ? it should be a protocol issue. i got this issue just yesterday with some cUrl request, because the SSL certificate does not been activated on my API server – Sim1-81 Sep 20 '19 at 14:35
  • @Lil' Bits Another thing, if you can, look out at the code which receive the request, maybe for some reason it would be looped – Sim1-81 Sep 20 '19 at 14:42

1 Answers1

3

So after about a month of struggling with this, I ended up using the Cordova Advanced HTTP plugin: https://www.npmjs.com/package/cordova-plugin-advanced-http

Using the setRequestTimeout method, I was able to increase the HTTP request timeout to 120 seconds:

cordova.plugin.http.setRequestTimeout(120.0);

After rewriting my requests to utilize this plugin, my issues were solved for Android 9/10, and everything still worked as expected for iOS 12/13.

Lil' Bits
  • 898
  • 2
  • 9
  • 24