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 setTimeout not working on iPhone - JSON API Call times out over 3G

Hi guys,

I'm working on a proof-of-concept for my boss, so any help would be very appreciated.

My app calls does a JSONP call to a RESTful API. Over Wi-fi the app works flawlessly both in emulator and on the device.

When using SpeedLimit to restrict the bandwidth on the computer or trying to run the app over 3G on the phone, the calls time out 100% of the time.

As of today, I'm using 1.4.2. I was under the impression the setTimeout problem supposed to be fixed for the iPhone in this version. Is this not the case?

I've tried passing the timeout value as an integer, string and within brackets (as an array?) but it doesn't seem to make a difference.

I've also tried implementing pretty much every one of home-grown timeout alternatives from this forum to no avail.

If someone has a solution, I'd be very interested in hearing it. Thank you for your help in advance.

— asked September 28th 2010 by Antti K.
  • api
  • httpclient
  • iphone
  • json
  • restful
  • settimeout

1 Answer

  • Accepted Answer

    In my testing it appears that you must set the timeout right after the constructor but before the actual call to open.

    This works

      var xhr = Titanium.Network.createHTTPClient();
      xhr.open("POST", url);

    But this doesn't seem to.

      var xhr = Titanium.Network.createHTTPClient();
      xhr.open("POST", url);

    I assume the reason is that the open method needed the timeout value to have been set so it knew what the timeout is for that call. If that's truly the case, the documentation ought to be updated to reflect it.

    BTW - This works for me in 1.4.1 and 1.4.2. It looks like the default timeout is around 15 seconds on iOS deveices and my calls had returned in less than that time but some have gone one for 40-50 seconds and the timeout being set to 60000 worked for me.

    — answered September 28th 2010 by John McKnight
    1 Comment
    • John,

      Thanks for your answer, it resolved my problem. And now that you mention it, it makes sense to set the timeout before even opening the connection.

      I agree, the documentation should be updated to reflect the correct order of methods. It is frustrating to have spent days on this just to find out the solution was that simple (or there was no real problem in the first place).

      — commented September 29th 2010 by Antti K.
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.