Android Tutorial Download huge files with HttpUtils2

Discussion in 'Tutorials & Examples' started by Erel, Jun 13, 2013.

Thread Status:
Not open for further replies.
  1. David Hawkins

    David Hawkins Member Licensed User

    Is there an upload component as well?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Are you uploading files to a Http server? Which format does your server expect?
     
  3. David Hawkins

    David Hawkins Member Licensed User

    Hi Erel

    Yes I am uploading to a Http server and the files are simply text files (CSV).
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    There isn't a single standard for uploading files to Http servers. The first step is to understand which format is expected by your server.
     
  5. mjtaryan

    mjtaryan Active Member Licensed User

    I'm unable to get this to work. I'm testing trying to download multiple files. I get no error messages and the log entries I've added indicate the files get downloaded. When I check the destination directory (for the test it is DirDefaultExternal), the files are there, but the device indicates they are somehow corrupted or empty.

    I've tried to replace the HTTP library (Version 1.26) with the version provided in your link in the original post. However, as I indicated in a post in that thread, when the HTTP library finishes loading it continues to indicate Version 1.26. I don't know if this is the problem, but need to get it fixed in order to download data to my app.

    The attached is the test project.
     

    Attached Files:

  6. Erel

    Erel Administrator Staff Member Licensed User

    You are not closing the output stream after calling File.Copy2.

    There are other problems in your code as well. You should move all the non-UI global variables to process_globals and only load the data when FirstTime is True.

    The timer is also not needed.
     
  7. mjtaryan

    mjtaryan Active Member Licensed User

    Thanks Erel,

    I completely forgot about closing the output stream.

    I put this together quickly - mostly rearragning your code. I was trying to get a handle on how I'd handle downloading a group of files.

    I changed the code as you indicated. The files now download, but there are still some problems.

    1. I've downloaded three types of files -- jpg, txt and html. Although the files work in a test app I wrote, none of the commercial apps I tried appear to recognize them either for what they are or that they have content (size > 0).
    2. I tried downloading the group using a loop and without the timer, but found that only the last file gets saved. It may be I'm not constructing the loop properly or the files are too small and get overwritten by the next before they're written out. I was having that problem prior to my original reply here. The timer makes the group download work and displays the progress sufficient for a large group of small files.

    I started exploring this approach because I wasn't having success with the standard Http methods such as "GetString" for downloading an html file. Even when I tried doing that with a single file, it never got saved whereas the ordinary plain text file I tried did.

    Any insights? Thanks again.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload your updated code?

    2) The problem is not in GetString. The problem is somewhere in your code.
     
  9. mjtaryan

    mjtaryan Active Member Licensed User

    I apologize for giving the impression GetString was at fault. I was merely saying the html file wasn't writing to the destination directory. I started playing with the http utilities because in none of my programming, whether for mobile devices or other systems, have I had the need to download files from an off LAN/WAN location. That need has now arrived and I needed to learn how to successfully accomplish that.

    I redid the original test with GetString and both the txt and html file get saved. However, both files may have illegal characters. But I don't know what they might be, which is why I haven't tried either Download2 or GetString2. I started exploring the large file download method in an attempt to avoid this issue. With the latter, all the files get downloaded. A minor drawback is that, whether using GetString or the bytestream method, the files can be displayed in my app but won't open in other commercial apps such as the device's browser (for the html file), a note editor (for the txt file) or the image viewer (for a jpg file). Like I say, that is a minor issue because I don't really intend for the downloaded files to be used except by the app I'm working on.

    As you requested I've attached the original test. Thanks.
     

    Attached Files:

    ibra939 likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this. It is not related to this tutorial.
     
    ibra939 likes this.
  11. Felix Maria

    Felix Maria Member Licensed User

    U make life Easy....:)
    The program downloaded even a 30mb file very quickly :)and the event triggred.
    I tried to copy the file with the code
    Code:
    Sub dd_Complete(Job As HttpJob)
        
    Log("Job completed: " & Job.Success)
        Job.Release
        
    Dim o As OutputStream
        o = 
    File.OpenOutput(File.DirDefaultExternal, "l.bal"False)
        
    File.Copy2(Job.GetInputStream, o)
        o.Close
    End Sub
    I am getting this error in the GetInputStream


    What could be the mistake?

    Many Thanks.

    Maria.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    You should call Job.Release only after you copied the downloaded file.
     
  13. Felix Maria

    Felix Maria Member Licensed User

    Thank you Erel. Corrected and its working perfectly now.
     
  14. antsab

    antsab Member Licensed User

    I've a little error with this, a file of 363,11 kb is only 360 kb on Android device and i give an error when i try to open (zip or jpg), have some tips?
    Thanks
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Compare the actual number of bytes. Also remember that 1kb = 1024 bytes.
     
  16. antsab

    antsab Member Licensed User

    Erel first of all.... thank you exist.
    I try to download again the example and now works fine!!!! :D
     
  17. Kwame Twum

    Kwame Twum Active Member Licensed User

    Guys, is there a group of file types that are compatible with the DownloadService? Anytime I try to download files like .apk, .amr, 3gpp, etc with my app, I get "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. (Exception) Not initialized"
    when I know they're right there on the server (#trust me, I put them there). Files like .txt, .zip, .mp3, .pdf and others however download nicely.
    Could there be a problem? Thanks in advance!
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    The problem is not in HttpUtils2 or DownloadService (which don't know anything about the file downloaded). It is probably something with your server configuration.
     
  19. Kwame Twum

    Kwame Twum Active Member Licensed User

    thanks Erel, I'll check that...
    However, FileZilla on my computer is able to download all files without any problem, so I'm sure the problem is not coming from the server
     
    Last edited: Feb 9, 2014
  20. Erel

    Erel Administrator Staff Member Licensed User

    FileZilla is a FTP client. It is not related.
     
    Kwame Twum likes this.
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice