Android Tutorial Download huge files with HttpUtils2

Status
Not open for further replies.
Better to use: https://www.b4x.com/android/forum/threads/simple-progress-http-download.127214/#post-796463

The attached project includes a slightly modified version of HttpUtils2 and a new service named DownloadService. The purpose of DownloadService is to make it simple to download files of any size.

SS-2013-06-13_17.34.35.png


It is very simple to use this service.

Start a download:
B4X:
Sub btnDownload_Click
   Dim dd As DownloadData
   dd.url = link1 '<--- download link
   dd.EventName = "dd"
   dd.Target = Me
   CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub

Handle the events:
B4X:
Sub dd_Progress(Progress As Long, Total As Long)
   ProgressBar1.Progress = Progress / Total * 100
   Label1.Text = NumberFormat(Progress / 1024, 0, 0) & "KB / " & _
      NumberFormat(Total / 1024, 0, 0) & "KB"
End Sub

Sub dd_Complete(Job As HttpJob)
   Log("Job completed: " & Job.Success)
   Job.Release
End Sub

Cancel a download:
B4X:
Sub btnCancel_Click
   CallSubDelayed2(DownloadService, "CancelDownload", link1)
End Sub

DownloadService allows you to download multiple files at once and track the progress of each one of them.

The following libraries are required:
OkHttp
StringUtils
Phone (required in order to acquire a partial lock during the download)
RandomAccessFile

As this is a modified version of OkHttpUtils2 you should not reference OkHttpUtils2 library. You should instead add the three modules from the attached project. Note that the modified code is in Sub hc_ResponseSuccess.
 

Attachments

  • LargeFileDownload.zip
    11.5 KB · Views: 2,874
Last edited:

mjtaryan

Active Member
Licensed User
Longtime 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.
 

Attachments

  • WebDownloadTest.zip
    6.5 KB · Views: 962

mjtaryan

Active Member
Licensed User
Longtime 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.

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.
 

mjtaryan

Active Member
Licensed User
Longtime 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.
 

Attachments

  • OriginalDownloadTest.zip
    1.9 KB · Views: 856

Felix Maria

Member
Licensed User
Longtime 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
B4X:
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

java.io.FileNotFoundException: /data/data/b4a.example/cache/1: open failed: ENOENT (No such file or directory)


What could be the mistake?

Many Thanks.

Maria.
 

antsab

Member
Licensed User
Longtime User
Thank you Erel. Corrected and its working perfectly now.

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
 

Kwame Twum

Active Member
Licensed User
Longtime 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!
 

Kwame Twum

Active Member
Licensed User
Longtime 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:
Status
Not open for further replies.
Top