Titanium Community Questions & Answer Archive

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

HTTPClient Not Refreshing Data - Android Only

I have an app that is reading an XML file off of our webserver. Depending upon the URL parameters that are sent to server, the XML either has 10 data entries or 1. When the app loads, all 10 records are shown, and users have a search bar they can use to enter in new data to search for.

The problem that I'm having is that on Android (it works fine on the iPhone):

  1. I create the HTTPClient, load it with data, when the app starts. I have 2 HTTPClients below, just to confirm that one URL returns 10 entries, the other returns just 1.

  2. When the user does the search, I reuse the HTTPClient with the new URL (strURL2 below).

  3. On the onLoad event for the HTTPClient, it still indicates that 10 entries are present, even through I know the XML only has 1.

  4. The result is put into a tableview (I cut that code out of below, I was just trying to show the minimum to recreate the problem).

You should be able to copy & paste this into any app.js to see the problem in action. Again, it works fine in iPhone emulator, but fails in Android.

// Local misc. variables
var strURL = 'http://www.cermusa.francis.edu/test/pharm/pharmxml.asp';
var strURL2 = 'http://www.cermusa.francis.edu/test/pharm/pharmxml.asp?search=astm';

var xhr = Titanium.Network.createHTTPClient();
var xhr2 = Titanium.Network.createHTTPClient();
xhr2.onload = function()  
{
    var doc2       = xhr2.responseXML.documentElement;
    var elements2  = doc2.getElementsByTagName("ticker");
    Ti.API.info('elements2.length: ' + elements2.length);
};

xhr.onload = function()  
{
    var doc       = xhr.responseXML.documentElement;
    var elements  = doc.getElementsByTagName("ticker");
    Ti.API.info('elements1.length: ' + elements.length);
};

xhr.open('GET', strURL);  //URL to XML file
Ti.API.info('data sent');
xhr.send();

xhr2.open('GET', strURL2);  //URL to XML file
Ti.API.info('data sent');
xhr2.send();

var search = Titanium.UI.createSearchBar({  //Search bar
    barColor:'#000', 
    showCancel:true,
    top:0,
    height:43
});

//Search button click listener - change the query for the data and reload the table view.
search.addEventListener('return', function(e)
{
    Ti.API.info('In Search');
    data = new Array();
    xhr.open('GET', strURL2);
    xhr.send();
});

// add table view to the window
var win1 = Titanium.UI.createWindow({
    title:"Main Window"});
win1.add(search);
win1.open();

Any help is appreciated…

— asked September 17th 2010 by Mike Shanafelt
  • android
  • httpclient
  • mobile
  • osx
0 Comments

3 Answers

  • Instead of using the variable name xhr for the callback, use this. Since this is the xhr class.

    xhr.onload = function()  
    {
        var doc       = this.responseXML.documentElement;
        var elements  = doc.getElementsByTagName("ticker");
        Ti.API.info('elements1.length: ' + elements.length);
    };
    
    — answered January 7th 2011 by Ali Almahdi
    permalink
    0 Comments
  • I am having the exact same problem. I am already using "this" in my onload functions though, so it must be something else.

    I found that if I recreated the HTTPClient at the end of each request, it handles the problem. Definitely a hack, but at least it works.

    — answered January 8th 2011 by Mike Dosey
    permalink
    1 Comment
    • can you post your code to explain how you achieved it or how you made it work. Thanks

      — commented June 28th 2013 by Thuan Pham
  • you dont mention what version of software you are using, but I know for certain that in the 1.5.1 and in the 1.6 release they have addressed android HTTPClient issues.

    I was forced to go to 1.6 from the Continuous Builds to get my android application to work like the iphone version.

    — answered January 8th 2011 by Aaron Saunders
    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.