Problem with : Titanium.Network.HTTPClient.onload on android

You must Login before you can answer or comment on any questions.

i have a problem when i try to use on iphone this:

Titanium.Network.HTTPClient.onload
it works fine but the same line on android makes problem!!!

this is my code:

DatabaseManager.updateDatabaseFromHTTP = function(lastUpdateTime) {
        DatabaseManager.databaseHTTPService = Titanium.Network.createHTTPClient();
        DatabaseManager.databaseHTTPService.setTimeout(10000);
        DatabaseManager.databaseHTTPService.onload = function(e) {
            if (e.source.status == HTTP_OK) {
                // a trick for making json strings into json objects.
                try {
                    var serverResponse = eval("(" + e.source.responseData + ")");
                    if (serverResponse.Areas.length > 0)
                        DatabaseManager.updateDatabaseTable(serverResponse.Areas, "Areas", "AreaID");                                   
                    Ti.App.fireEvent("databaseLoadCompleted");
                }
                catch(e) {
                    Ti.App.fireEvent("databaseLoadFailed");
                }
            }
            else
                Ti.App.fireEvent("databaseLoadFailed");
        };
        DatabaseManager.databaseHTTPService.onerror = function(e) {
            Ti.App.fireEvent("databaseLoadFailed");
        };      
        DatabaseManager.databaseHTTPService.open("GET", "http://mobile90.tapuz.co.il/jobs/appsupdate/Trails/GetNewData.aspx?ld=" + lastUpdateTime + "&r=" + Math.random()*1000000);
        DatabaseManager.databaseHTTPService.send();
    }
when i debug this code is good on ihpne but on android i get on this line:
DatabaseManager.databaseHTTPService.onload = function(e)
 
//the response:
 
else
    Ti.App.fireEvent("databaseLoadFailed");
do you have a solution for me?

thanks, Eliza

— asked 12 months ago by Eliza Sapir
2 Comments
  • Which Titanium SDK version are you using?

    I have just discovered an issue with fireEvent in an app with version 2.1 (ci build) which worked fine in 1.9

    — commented 12 months ago by Kosso .

  • i am using 2.2

    — commented 12 months ago by Eliza Sapir

1 Answer

Here is the ajax code that I have used for over a year and works great on both platforms:

var utils = {
 
   ajaxInProgress: false,
 
   ajax: function (url, callback) {
 
        //TODO: queue up next request instead of not executing it.
        if (!utils.ajaxInProgress) {
            utils.ajaxInProgress = true;
            url = url.indexOf("http") > -1 ? url : (tt.config.url + url);
            Ti.API.info("ajax call: " + url);
            if (!utils.xhr)
                utils.xhr = Ti.Network.createHTTPClient();
            utils.xhr.setTimeout(20000);
            utils.xhr.onload = function () {
                callback(this.responseText);
                utils.ajaxInProgress = false;
            };
            utils.xhr.onerror = utils.ajaxError;
            utils.xhr.open("GET", url);
            utils.xhr.setRequestHeader("User-Agent", "Appcelerator");
            utils.xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
            utils.xhr.send();
        }
        else
            Ti.API.info("ajax request in progress so not making new one");
 
    },
 
   ajaxError: function (e) {
 
        utils.ajaxInProgress = false;
        Ti.API.info(JSON.stringify(e));
        Ti.API.info("ajax error: " + e.error);
 
    }
}

Your Answer

Think you can help? Login to answer this question!