Titanium.Network.HTTPClient.setTimeout() blocks open/send;
Hi,
In response to another issue with Ti.Network.createHTTPClient() timeout, a user suggested I work with Titanium.Network.HTTPClient.setTimeout(). If I set the value with some code such as:
Ti.Network.HTTPClient.setTimeout(5000);
…
xhr.open('GET', url);
xhr.send();
The request is never sent. If I comment out the setTimeout(), it works normally. The 5000 should represent 5 seconds, argument is milliseconds.
Any ideas?
Thanks,
Bruce
8 Answers
-
I had this same problem today. My xhr request was finishing when uploading images, but the xhr.onload was never firing. I got around it by adding a custom setInterval to check the ready state of the xhr object. Below is what I did:
HflApi.prototype.uploadImage = function(url, image, callback) { var xhr = Titanium.Network.createHTTPClient(); var timeout = setInterval(function() { if (xhr.readyState == 4) { clearInterval(timeout); callback("\"Success\""); } }, 1000); xhr.onload = function() { callback(this.responseText); } xhr.open("POST", url); xhr.setRequestHeader('Content-Type', 'form-data'); xhr.send({media:image}); }
Basically what I am doing is checking if the readyState has changed to 4 which I am assuming means it is done. If it is then I manually fire my callback function, rather than waiting for the onload that never fires.
-
Hi Everyone,
Turns out you need to use an array in the method argument. Not sure why this is so, since you are only submitting one value. This works, although I haven't checked if the client really waits 10 seconds:
var xhr = Ti.Network.createHTTPClient();
xhr.setTimeout([10000]);
Bruce
-
This works great for me (Android):
var xhr = Ti.Network.createHTTPClient(); xhr.setTimeout(99000); xhr.onload = function() { Ti.API.info('got my response, http status code ' + this.status); }; xhr.open('GET', 'http://www.billdawson.com/feed/',false); xhr.send();
So I set the timeout for 99 seconds, but I get my response right away.
Maybe if you posted more of your code? Like, you are setting an onload handler (or onreadystatechange), right?
-
Just to confirm - I'm hitting the same wall with iphone 3.1 & titanium 1.0
xhr is timing out before I get the response back from my (very slow) server… trying to set the httpclient's setTimeout option gives me the same error as above
Is this recognized as a bug?
thanks!
DH -
The method with the setInterval is not that good because I also get an readyState 4 when I send an abort. So that don't works very well.
The solution with the box brackets also doesn't work because I get an error that box errors are not allowed.
With luck I now found out that if you use an an setInterval that is only checking the readyState, onload is called. Don't ask me why, but it works. Looks like a bug.
Here is the code:
onloadInterval = setInterval(function(){ if(c.readyState == 4){ clearInterval(onloadInterval); } },5000);
-
try this.
xhr.onreadystatechange = function() { //alert('xhr.readyState: ' + xhr.readyState); if(xhr.readyState == 4) { loaded(this.responseText); } }
-
Hi Bill,
I posted your exact code into a new window in my application. I'm running 1.0 with the iphone simulator. The iphone code apparently doesn't recognize this method. Here's the exception when I run your code from the console:
2010-03-11 10:13:04.822 LeanScale[1769:530b] -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x522c3b0
[ERROR] Script Error = -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x522c3b0 at login.js (line 1).Thanks for trying.
Bruce
-
Hi Bruce,
I see you posted the other item. I'll leave it "unanswered", so that the Appcelerator guys see it more easily. (I have a feeling they check for unanswered questions first.)
I did, however, want to tell you that I made a mistake in my code sample to you: I thought the third argument to HTTPClient.open was "synchronous", not "asynchronous". But in fact it's vice-versa. So the code that I gave you actually blocks and waits for a response. I should have just not included the third argument, because the default is to make it asynchronous. So here's my fixed code, though it won't help you because (having now tried it on my Mac in the iPhone simulator), I too get the same error that you're getting and reported in the other item.
var xhr = Ti.Network.createHTTPClient(); //xhr.setTimeout(9000); xhr.onload = function() { Ti.API.info('got my response, http status code ' + this.status); }; xhr.open('GET', 'http://www.billdawson.com/feed/', true); xhr.send();