Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-2496] Android: Titanium.Network.HTTPClient.onload fired with a delay!

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionInvalid
Resolution Date2011-04-15T03:21:19.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.6.0 M01
ComponentsAndroid
Labelsandroid, defect, httpclient, release-1.6.0
ReporterFarsad
AssigneeDon Thorp
Created2011-04-15T03:21:17.000+0000
Updated2017-03-02T19:28:34.000+0000

Description

Titanium.Network.HTTPClient.onload fired with a delay! We tested it with the on the Iphone and some android phones. It hasn't this problem on the Iphone(Iphone fire it on 2 seconds). But on my Samsung Galaxy S With android 2.2 on wifi it takes 10 to 16 seconds. On a HTC Wildfire with wifi or 3G network it takes between 3 to 5 minutes and sometimes not fired or takes longer! This issue reported on a HTC Desire too. It seems this problem is on first run of the app or when we clear cache and data of the app.
Also I found a same reported problems on the developer center:
http://developer.appcelerator.com/question/881/titaniumnetworkhttpclientsettimeout-blocks-opensend"> http://developer.appcelerator.com/question/881/titaniumnetworkhttpc...

I tested it by Titanium SDK 1.4.1 and 1.5 and I'm sure that the server answer to this requests quickly (We use google servers with the appengine) and that codes work fine on the Iphone.
We use this codes:

var SessionID = '';
Ti.App.addEventListener('nAjax', function(e)
{

var xhr = Ti.Network.createHTTPClient();

xhr.onerror = function(r) {
    trace('nAjax.onerror('+e.error+'): ' + r.error);
    if (e.error) {
        Ti.App.fireEvent(e.error,{error:r.error, status:xhr.status});
    }
    if (e.complete) {
        Ti.App.fireEvent(e.complete,{status:xhr.status});
    }
};

xhr.onreadystatechange = function() {
    trace('nAjax.onreadystatechange('+e.success+') xhr.readyState: ' + xhr.readyState);
}

xhr.onload = function() {
    trace('nAjax.onload('+e.success+') xhr.status: ' + xhr.status);
    if (e.success) {
        cookie = xhr.getResponseHeader('Set-Cookie');
        if (cookie){
            if (cookie.indexOf('sessionid') > -1) {SessionID = cookie.substring(cookie.indexOf('sessionid')+10, cookie.indexOf(';'));}
        }

        Ti.App.fireEvent(e.success,{response:this.responseText, status:xhr.status});
    }
    if (e.complete) {
        Ti.App.fireEvent(e.complete,{status:xhr.status});
    }
};

xhr.setTimeout(30000);
xhr.open(e.method, e.url, false);
if (e.username){
    Ti.API.info('xhr.setRequestHeader');
    xhr.setRequestHeader('Authorization','Basic ' + Ti.Utils.base64encode(e.username+':'+e.password));
}
if (Titanium.Platform.osname == 'android'){
    if (SessionID.length > 0) {xhr.setRequestHeader('Cookie',('sessionid=' + SessionID + ';'));}
}
//send the data
xhr.send(e.data);
trace('nAjax.send');

});

Attachments

FileDateSize
app.js2011-04-15T03:21:17.000+00001158

Comments

  1. Don Thorp 2011-04-15

    @Farsad can you put your sample code into a small project that reproduces the failure including a url we can hit to test with.

  2. Opie Cyrus 2011-04-15

    Hi Farsad,

    I am unable to replicate the delay on 1.5 using the 2.2 API's. I have attached the contents of the app.js for the simple test I wrote trying to replicate the issue. I have also been unable to reproduce this behavior on the droid 2 (running 2.2 with and without wifi). I also made sure to clear the data and cache before running the test.

    Can you please provide a simple test project and url as Don mentioned? Thanks in advance.

  3. Opie Cyrus 2011-04-15

    Attached app.js as promised in previous update.

  4. Farsad 2011-04-15

    Thanks for your responds.

    I can't replicate this issue on a sample test app! I try to find the reason of this problem on our app. our app codes are same as created sample app but doesn't work correctly.

    this is a trace of my app for a request:

    5:33:8 trace: nAjax.send
    5:33:8 trace: nAjax.onreadystatechange(AjaxSuccess1) xhr.readyState: 1
    5:39:29 trace: nAjax.onreadystatechange(AjaxSuccess1) xhr.readyState: 2
    5:39:29 trace: nAjax.onreadystatechange(AjaxSuccess1) xhr.readyState: 3
    5:39:31 trace: nAjax.onreadystatechange(AjaxSuccess1) xhr.readyState: 4
    5:39:31 trace: nAjax.onload(AjaxSuccess1) xhr.status: 200

    Maybe this trace shows that server didn't respond to the request. but we have a 30 seconds timeout and we didn't get onerror trace or any message. and server responds to the requests for the sample test app or our app on iphone quickly.
    Also the reports on the server shows that got request at 5:33:41 in about 35 second delay and responded to it after 446ms.

    I'm confused! I working to find codes that makes different between sample test app and our app!

  5. Opie Cyrus 2011-04-15

    Hi Farsad,

    If the trace above is generated from the app in question, then the onload is being called with minimal delay once the request is actually sent. The delay issue seems to stem (in the above trace at least) from a 21 second delay between the open state (1) and the sent state (2).

    What is the URL you are trying to hit? Were you able to reproduce the behavior on the device in question with a sample app using the app.js I provided?

    Thanks.

  6. Farsad 2011-04-15

    I found the problem!
    I was put a loading animation on the registration and login buttons by a CSS3 animation in our app. It was a small gif picture that animated by this CSS3 code:

    .loadingAnimation { / -webkit-animation-name: rotateThis; -webkit-animation-duration:.7s; -webkit-animation-iteration-count:infinite; -webkit-animation-timing-function:linear;/ } @-webkit-keyframes rotateThis { / from {-webkit-transform: rotate(0deg);} to {-webkit-transform: rotate(360deg);}/ }

    I removed this and the problem solved! maybe it was a hug process for some android phones!

    Special thanks for your supports.
    Please close this topic and ban me! ;)

  7. Don Thorp 2011-04-15

    Closing as invalid per the submitters request.

  8. Lee Morris 2017-03-02

    Closed as invalid.

JSON Source