Search code examples
androidxmlcordovaxmlhttprequestphonegap

Cordova Android Update Broke XMLHttpRequest Downloads


I am working with Cordova Android.

I updated my Android SDK and removed then added my android platform.

cordova platform rm android
cordova platform add android

Now my XMLHttpRequest download script no longer works... I don't need help with the script as I know it works as I was using it directly before the update. Also checked my server logs and there are no errors. The files can also be accessed by directly accessing the server through a browser.

var dlUrl = "http://myserver.com/" + fileName;
var fileSystem = cordova.file.externalApplicationStorageDirectory;

window.resolveLocalFileSystemURL(fileSystem, function (dir) {
dir.getFile(fileName, {create: true}, function (file) {

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", transferCompleteDb);
oReq.responseType = "blob";
oReq.open("GET", dlUrl, true);
oReq.onload = function (oEvent) {
var blob = oReq.response; 

if (oReq.status == '523' || oReq.status == '404') {
   //...
} else {
if (blob) {
    var writeToFile = saveFile(file, blob);
} else {
//...            
} } };

oReq.onloadend = function () {
   //...
};

oReq.onerror = function (e) {

alert('xml error. status [' + e.target.status + '] [' + oReq.status );
//***
//*** This is where it fails. The error code is just 0, no other info  
//     The request isn't even getting a chance to get a response
//      Cordova is blocking it in some setting              
};

oReq.send(null);
//...

Cordova is blocking the download through some setting I am missing...

Are there any settings I should manually change inside the Android platform for newer versions?

My index header, unchanged from before the update

<meta http-equiv="Content-Security-Policy"
      content="default-src 'self' myserver.com http://myserver.com data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">

I also tried just using * for all values to open it wide open but made no change

From what I understand permissions are now prompted inside the app and I should be getting some kind of prompt if a permission is needed for the download. I have completely uninstalled and erased storage for the old app several times over before trying the new builds that will no longer work.

My other scripts are able to create folders and files in the area I am storing the download. So it is just the XMLHttpRequest code that can't be called.

Any suggestions? Spent a few days battling this one...

Thanks.


Solution

  • Problem solved...

    The Cordova update changed it so that XMLHttpRequest can only handle links with https. So all my links that used http stopped working, and of course threw no errors as that is the joy of working with Cordova. Wasted a full week on this one. Hopefully this can help someone with the same problem.

    Also, your server doesn't actually have to serve https... Cordova is happy if you just add https to your http links and it will work fine.