Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Can't get XHR to work

I'm trying to download an XML file on a remote server and save it locally as an XML file for my application to use offline. However, the 'xhr.onload' function always fails, but when it shows the 'Application Error' alert dialog, the actual error is blank, so I have no idea what's wrong. Here is the code I'm using:

    if (Titanium.Network.networkType != Titanium.Network.NETWORK_NONE){ // Network is available, download latest database
        var xhr = Ti.Network.createHTTPClient();
        xhr.open("POST","http://www.5byfive.net/vehicles.xml");
        xhr.onerror = function(e) {
            Ti.UI.createAlertDialog({title:'Network Error', message:e.error}).show();
            Ti.API.info('IN ERROR ' + e.error);
        };
        xhr.setTimeout(30000);
        xhr.onload = function() {
            try {
                var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'vehicledata.xml');
                var doc = this.responseXML.documentElement;
                if (doc.getElementsByTagName("success").item(0).text == 1) {
                    f.write(this.responseData);  
                } else {
                    Ti.UI.createAlertDialog({title:'Service Error', message:'Service is not available at the moment, please try again later'}).show();
                };
            }
                catch(E){Ti.UI.createAlertDialog({title:'Application Error', message:E.error}).show();
            };
        };
        xhr.send();
    };

I guess it's either a problem with getting the file from the remote server, or writing it to a local file?

— asked June 1st 2010 by Bill Labus
  • download
  • file
  • network
  • xhr
0 Comments

2 Answers

  • Accepted Answer

    Looking at the xml file, i don't see you a node name success, why request it?

    Try remove that line and the subsequent else line, just do

    try {
    var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'vehicledata.xml');
    f.write(this.responseData);
    Ti.API.info(this.responseText); // check the file content on TI info panel.
    }
    catch......
    

    ON OSX, You can check the physical downloaded file on user/library/application support/iphone simulator/{titanium SDK version}/application/{app long string of unique id}/Documents/yourfile.xml

    — answered June 1st 2010 by Daniel Lim
    permalink
    0 Comments
  • Well, the XML at that URL pulls up just fine, so that's not an issue

    one thing that stands out right away after looking at the XML:

    • You are looking for a tag by the name of "success"… but the XML file contains no such name
    — answered June 1st 2010 by Stephen Gilboy
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.