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:

cbal03

Member
Licensed User
Longtime User
Since your ftp client can download them and your app can't.. Possibly there is a permissions issue with your server setup. The FTP server has permission to send to client but does your app have permissions in the manner that it is connecting?
 

Kwame Twum

Active Member
Licensed User
Longtime User
Thanks guys... Erel , I found out the mime types on my server were only set to default file types. I manually included the ones that were causing issues (.apk, .amr, etc). Now my app downloads just fine. @cbal03 so set Full Control permissions... just in case ;) Thanks a lot.
 

aminoacid

Active Member
Licensed User
Longtime User
I modified the URL in the "LargeFileDownload" program shown in your Tutorial "Download Huge Files with HttpUtils2". Everything works fine.

Then I intentionally changed the name of download file to a non-existant file in an attempt to see what sort of error message I would get. So I get the following error message.

----------------------------------
An error has occurred in sub:
java.io.FileNotFoundException:/data/data/b4a.example/cache/1:eek:pen failed: ENOENT (No such file or directory) Continue?
----------------------------------

I tried using Try-Catch at various places in the program so I could trap this error and display a more informative message but I can't seem to trap this error.

Any suggestions on what I can do to trap it?
 

Penfound

Active Member
Licensed User
Longtime User
The file is downloaded to a temporary folder. You should use File.Copy2 together with Job.GetInputStream to copy the file.

I still can't find the file that has been downloaded. How do I download it and copy it to a specified folder?

Please?

Penfound
 

Bart Kerver

New Member
Licensed User
Longtime User
I created a (huge) file uploader that basically works fine (based on the download demo, pretty straight foreward).

In order the give feedback on the upload status (when using GSM/UTMS), I tried to create a bytes sent in some way.

Is there a way to get the amount of bytes sent (from within the httpclient object), since for the post you only pass the HttpRequest object and the file, the rest is done automatically. The CountingStream in the demo works different since that's done by the receive.

Or do I need to abandon the standard httpclient and work with the async and create own http protocol (hand-shake, error-code etc) for that?

I looked into tapping into the httpclient object using the Reflector but it seems to pretty well hidden ;)

Thanks,
Bart
 

Callan

Member
Licensed User
Longtime User
Hi Erel,
I'm tried the sample for this and I got this
error on debug.

B4X:
Parsing code.                          0.02
Compiling code.                        0.36
Compiling layouts code.                0.17
Generating R file.                      0.13
Compiling debugger engine code.        Error
javac 1.7.0_51
shell\src\b4a\example\main.java:31: error: package anywheresoftware.b4a.pc.RapidSub does not exist
        anywheresoftware.b4a.pc.RapidSub.moduleToObject.put("b4a.example.main", "b4a.example.main");
                                        ^
1 error

I haven't change any codes yet just a fresh debug. If so, what extra codes are necessary to evade this issue.
Cheers,
Callan
ps. I've got the updated version of http (v1.33)
 

Callan

Member
Licensed User
Longtime User
I tried it with debug legacy and it works seems to be an issue with V3.8 then....I'd also like to know how will I be able to store the file it in a folder in the ext card rather than just the root...
Here is my code
B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    Dim o As OutputStream
    o = File.OpenOutput(File.DirDefaultExternal, "test.zip", False)
    File.Copy2(Job.GetInputStream, o)
    o.Close
    Log("Job completed: " & Job.Success)
    Job.Release
End Sub
 

Callan

Member
Licensed User
Longtime User
Hey Erel,
Just a quick question is it possible to download a whole subfolder?
If not what would be the best way to do it.
Cheers
 

RickyT

New Member
Licensed User
Longtime User
Hi i'm getting an error while doing job complete... This is the code
B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    Dim o As OutputStream
    o = File.OpenOutput(File.DirDefaultExternal, "test.txt", False)
    File.Copy2(Job.GetInputStream, o)
    o.Close
   Log("Directory : " & File.DirDefaultExternal)
    Log("Job completed: " & Job.Success)
    Msgbox("Download completed","Success")
    Job.Release
End Sub

And this is the error code
B4X:
java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=localhost/ComiX/30DaysOfNight/
Job completed: false

Could I please get some help with this please :)
 
Status
Not open for further replies.
Top