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,892
Last edited:

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, First off this is my first post on the forum, I purchased a couple of days ago! My only experience in programming was AMOS Basic on the Commodore Amiga, And a breif attempt in VB on windows 3.11! So I wanted to try my hand but had a look at this JAVA stuff, and to be honest even the IDE scared me! lol, So many files for such small tasks! I found this Basic4Android And Wow! I have made my first app and launched it! works really well! Thanks for making Basic4Android! I Love it!

So to the question! I know A little basic, and my App is basically the largefilesdownload example but with added stuff! But I dont understand a few things! in this example below

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

Q. After the job is released, how do I know that the download was successful?

Q. Log, Where is it? How do I read it?

Thanks once again for such a superb and easy to use, IDE and language!
 

aidymp

Well-Known Member
Licensed User
Longtime User
You should work with the job in this sub, before it is released. I recommend you to go over HttpUtils2 tutorial to understand how HttpJob works.

I have a few times but im still very new, lol. So the fact that the Job has A) downloaded and B) Saved the file should be saved to some global variables that I can check outside the sub? is that about right??

The logs appear in the Logs tab at the right side of the IDE. If you are using B4A-Bridge then they will only appear in debug mode.

Oh! lol I was looking on the device for a log file! Silly me!

Thanks and expect a lot more quiestions in the coming years!

Greatly appreticate this program, and your AMAZING forum!
 

ibra939

Active Member
Licensed User
Longtime User
Thanks you Erel
 

ibra939

Active Member
Licensed User
Longtime User
is it current ?
1- i did button code as follow :

Sub btnCancel_Click
'If Select
' Case:1
CallSubDelayed2(DownloadService, "CancelDownload", link1)
' Case:2
CallSubDelayed2(DownloadService, "CancelDownload", link2)

'End If
End Sub


---------------------------------------------


2- all button :


Sub Button2_Click
Dim dd As DownloadData
dd.url = link3
dd.EventName = "dd"
dd.Target = Me
CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub

Sub Button1_Click
Dim dd As DownloadData
dd.url = link1
dd.EventName = "dd"
dd.Target = Me
CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub


--------------------------------------------------------


3 - link

Sub Process_Globals
Private link1 As String = "http://example.com/a.mp3"
Private link2 As String = "http://example.com/b.mp3"



the problem when i download is working but i can't find the file ?
can i get help ? is there any problem in code ?
 

ibra939

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.
how can i use file.copy2 ? to find my download file ?
 

Inman

Well-Known Member
Licensed User
Longtime User
I am using DownloadService to download an html file. After downloading it, I parse the file, remove some unnecessary code and display the rest in a webview. The reason I am using DownloadService and not HttpUtils directly is that I need to monitor the progress of the download and display it using a progressbar.

I am calling this particular activity that downloads and displays the HTML page as StoryView. The first time I start this StoryView activity and initiate a download using DownloadService, nothing happens. No progress data, nothing. In the log I see this message.
B4X:
sending message to waiting queue of uninitialized activity (startdownload)

If I close this activity, get back to main and open this activity again, everything works fine. It will continue to work until the DownloadService is stopped by the system. Once that happens, the issue repeats.

I believe the issue happens every time the service is started. What could be wrong?
 

Inman

Well-Known Member
Licensed User
Longtime User
@Inman where is StartDownload declared? Can you post the code that starts the download?

It is in the DownloadService which I downloaded from here. I don't remember making any changes to the code. Here is the code.
B4X:
Public Sub StartDownload(data As DownloadData)
    If jobs.ContainsKey(data.url) Then
        Log("Ignoring duplicate request.")
        Return
    End If
    Dim J As HttpJob
    J.Initialize(data.url, Me)
    Dim tag As JobTag
    tag.Initialize
    tag.data = data
    J.tag = tag
    jobs.Put(data.url, J)
    J.Download(data.url)
    If timer1.Enabled = False Then StartTimer(data.Target)
End Sub
 

Inman

Well-Known Member
Licensed User
Longtime User
I didn't modify. I used the following code from example.
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

And surprisingly it works fine from the second time with the same code. The error only happens when IsPaused(DownloadService)=True.
 

ibra939

Active Member
Licensed User
Longtime User
I didn't modify. I used the following code from example.
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

And surprisingly it works fine from the second time with the same code. The error only happens when IsPaused(DownloadService)=True.

working but u can't save file in download file will be good ???
 

ibra939

Active Member
Licensed User
Longtime User
HttpUtils2 saves the file to a temporary folder. You will need to modify it if you want to save it to a different location directly.


how i can change the location if i want save directly in download file ...?
 

ibra939

Active Member
Licensed User
Longtime User
thanks Erel so much i will try
 

sasidhar

Active Member
Licensed User
Longtime User
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:
Http v1.31+ the download will not be properly canceled with previous versions of Http library.
StringUtils
Phone (required in order to acquire a partial lock during the download)
RandomAccessFile

As this is a modified version of HttpUtils2 you should not reference HttpUtils2 library. You should instead add the three modules from the attached project. Note that the modified code is in Sub hc_ResponseSuccess.[/Q
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:
Http v1.31+ the download will not be properly canceled with previous versions of Http library.
StringUtils
Phone (required in order to acquire a partial lock during the download)
RandomAccessFile

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




Where the downloaded data shall be stored..
 

DonManfred

Expert
Licensed User
Longtime User
Where the downloaded data shall be stored..

Wherever you want... You must save them in Sub dd_Complete

See this example

B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    If Job.Success Then
        Dim tag As JobTag = job.Tag
        Dim dd As DownloadData = Tag.Data
        Log("Download of "&GetFilename(dd.url)&" finished...")
    Dim OutStream As OutputStream
    OutStream = File.OpenOutput(File.DirRootExternal, GetFilename(dd.url), False)
        File.Copy2(Job.GetInputStream,OutStream) ' save the file
       OutStream.Close
       Log(GetFilename(dd.url)&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)
    End If
    Job.Release
End Sub

Edit:
sorry i forgot a used sub of mine...

Add this sub too
B4X:
Sub GetFilename(fullpath As String) As String
   Return fullpath.SubString(fullpath.LastIndexOf("/") + 1)
End Sub
 
Status
Not open for further replies.
Top