Titanium Community Questions & Answer Archive

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

Problem with HTTPClient...is there a way to close properly the session ?

Hi,
I have one problem with the object HTTPClient.

On my app, I have a window where the user can download a file (by clicking on a button.
Here, the code I use for the download :

    httpClient = Titanium.Network.createHTTPClient();
    httpClient.setTimeout(10000);

     httpClient.onload = function()
          {
         Ti.API.info('on load');
             // Download is ok!!!
          };

       httpClient.ondatastream = function(e)
           {
              Ti.API.info('on data stream '+e.progress);
           };
       httpClient.onerror = function(e)
           {
               Ti.API.info('error '+e.error);
           };    

       if ( Titanium.Network.online ) {
        httpClient.open('GET',"http://myserver/myfile");
               httpClient.send();            
       }

This code works fine if the user waits that the download is complete, but if the user doesn't wait, and click for example on the back button while the file is downloading, the download is stopped, but there is an important problem when the user returns on the download page…If he didn't wait that download was complete, it is now impossible to start the download! I get a time out error, and I can't download the file anymore (I have to close the app, and re-open it, to be abble to download again…).

I've tried to use the abort function on the close event of my download window, but it doesn't work…
Do you know if there is a way to close properly an HttpConnection or something like that in order to resolve my problem ?

Thanks, for your help.

— asked September 15th 2010 by Guillaume Ferrand
  • httpclient
  • iphone
0 Comments

4 Answers

  • try to Provide Back button click option such that

    if (download is in progress)
    httpClient.close(); && delete the file if exists which is downloaded
    else
    provide back navigation procedure

    — answered September 15th 2010 by Varun Atluri
    permalink
    2 Comments
    • The problem is that the function close doesn't exist on the HTTPCLient object :-(

      — commented September 15th 2010 by Guillaume Ferrand
    • check status - the response HTTP status code & set time to abort the process

      — commented September 16th 2010 by Varun Atluri
  • close() no, but abort() does exists :)

    — answered September 15th 2010 by Dan Tamas
    permalink
    1 Comment
    • yes, but abort() doesn't work :-(

      — commented September 15th 2010 by Guillaume Ferrand
  • Can you add a random string to the end of the file?

    httpClient.open('GET',"http://myserver/myfile?"+ Math.random() );

    — answered September 15th 2010 by Dan Tamas
    permalink
    1 Comment
    • Yes, I've tried too, but it doesn't change anything…

      — commented September 16th 2010 by Guillaume Ferrand
  • I am facing the same issue but while uploading the file. any help? our problem is: we start file upload. if user wait till the file get uploaded then no problem. if user cancelled the upload and try to send any webservice to the same server it get timedout.

    — answered February 21st 2013 by chezhian p
    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.