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

ibra939

Active Member
Licensed User
Longtime User
Can you post the code?
#Region Project Attributes
#ApplicationLabel: SamaaIslam
#VersionCode: 1
#VersionName: 1.0
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
Private link1 As String = "http://moysar.com/download/molay.mp3"
Private link2 As String = "http://moysar.com/download/molay.mp3"


End Sub

Sub Globals
Dim ProgressBar1 As ProgressBar
Dim Label1 As Label


Dim btnCancel As Button
Dim OutStream As OutputStream
Private Button1 As Button
Private WebView1 As WebView
Private B1 As Button
Private Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
End Sub


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)
If Job.Success Then
Dim OutStream As OutputStream
Log("DownloadReady: "&Job.Tag)
OutStream = File.OpenOutput(File.DirRootExternal, Job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
File.Copy2(Job.GetInputStream,OutStream) ' save the file
OutStream.Close
End If
Job.Release
End Sub

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

'End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Dim dd As DownloadData
'dd.url =
' WebView1.LoadUrl("http://tvquranserver.com/download/TvQuran.com__Athkar/TvQuran.com_athkar_10.mp3")
dd.url = link1
dd.EventName = "dd"
dd.Target = Me


CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub
Sub Bn1_Click
Activity.RemoveAllViews
Activity.LoadLayout("Azker")
End Sub
Sub Button11_Click
' WebView1.LoadUrl("http://tvquranserver.com/download/TvQuran.com__Athkar/TvQuran.com_athkar_10.mp3")

End Sub

Sub Button2_Click
' WebView1.LoadUrl("http://tvquranserver.com/download/TvQuran.com__Athkar/TvQuran.com_athkar_01.mp3")
End Sub
 

ibra939

Active Member
Licensed User
Longtime User
you mean i add link1 to call the code Private link1 As String


or i add "http://moysar.com/download/molay.mp3"


Is't current following code i inserted ?


Sub dd_Complete(Job As HttpJob)
Log("Job completed: " & Job.Success)
If Job.Success Then
Dim OutStream As OutputStream
Log("DownloadReady: "&link1)
OutStream = File.OpenOutput(File.DirRootExternal, link1 ,False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job

File.WriteString(File.DirDefaultExternal,link1, Job.GetString)

Log(File.ReadString(File.DirDefaultExternal, link1))

File.Copy2(Job.GetInputStream,OutStream ) ' save the file
OutStream.Close
Else
Log("Error: " & Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
 
Last edited:

ibra939

Active Member
Licensed User
Longtime User
Erel new error thanks for you help......Iam trying to do it
 

DonManfred

Expert
Licensed User
Longtime User
Without posting the error you say you get it is hard to imagine what you are doing.....

LEARN TO WRITE GOOD HELP-REQUESTS!

- Post the code you are using (not part of them. better the project exported as zip)
- and the EXACT error you got (NOT A JPG, the error from log as text).
- describe when exactly you get this error and what you have tried so far (the code you tried)

With the infos you provided i just can use a wonderglas or even guess something which could be the problem! I don´t want to help with so much less informations provided!

.
.

For the others who read this here; i got an personal mail from @ibra939 to help with this problem and he gave me just the link to page 7 of this thread. That´s all... So i read the last post and read a "i´ll try it"... lol
 

ibra939

Active Member
Licensed User
Longtime User
Without posting the error you say you get it is hard to imagine what you are doing.....

LEARN TO WRITE GOOD HELP-REQUESTS!

- Post the code you are using (not part of them. better the project exported as zip)
- and the EXACT error you got (NOT A JPG, the error from log as text).
- describe when exactly you get this error and what you have tried so far (the code you tried)

With the infos you provided i just can use a wonderglas or even guess something which could be the problem! I don´t want to help with so much less informations provided!

.
.

For the others who read this here; i got an personal mail from @ibra939 to help with this problem and he gave me just the link to page 7 of this thread. That´s all... So i read the last post and read a "i´ll try it"... lol


I make the program so exported as zip file and when i found the errer i con't fix it because when i change job.tag give me same error i sent u because you have expert more then me also Erel he help me and i post the error code with the program .....o_O:eek::(
 

DonManfred

Expert
Licensed User
Longtime User
I make the program so exported as zip file and when i found the errer i con't fix it because when i change job.tag give me same error i sent u because you have expert more then me also Erel he help me and i post the error code with the program .....o_O:eek::(

Ok. then AGAIN cause i DONT WANT to search for any error you get (it´s your part to give us these information!):

- Post the code you are using (not part of them. better the project exported as zip)
- Post the EXACT error you got (NOT A JPG, the error from log as text).
- describe when exactly you get this error and what you have tried so far (the code you tried)
 

ibra939

Active Member
Licensed User
Longtime User
Ok. then AGAIN cause i DONT WANT to search for any error you get (it´s your part to give us these information!):

- Post the code you are using (not part of them. better the project exported as zip)
- Post the EXACT error you got (NOT A JPG, the error from log as text).
- describe when exactly you get this error and what you have tried so far (the code you tried)

Thanks for Replay DonManfred the file with Error log ( http://www.mediafire.com/download/iop3vdia8iv8zl9/project.zip )

jest i want the mp3 file download to memory SD or phone memory
 

Attachments

  • error.txt
    2.6 KB · Views: 407
Last edited:

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success )
  If Job.Success Then

        ' You are missing these lines....
        Dim JobTag As JobTag = job.Tag
         Dim dldta As DownloadData=JobTag.Data

    
        Dim OutStream As OutputStream
      Log("DownloadReady: "&dldta.url )
      ' -> DownloadReady: http://moysar.com/download/molay.mp3

      ' Please note the changes here too
      OutStream = File.OpenOutput(File.DirRootExternal,GetFilename(dldta.url),False) ' Getting filename with sub getfilename giving dldta.url to read the Original Filename we specify earlier in the creation of the Job

      File.Copy2(Job.GetInputStream,OutStream ) ' save the file
      Log(GetFilename(dldta.url)&" written to DirRootExternal")
      ' -> molay.mp3 written to DirRootExternal
        OutStream.Close
      ' The file should now be accessible in File.DirRootExternal
      ' Play the file now for ex.
     Else
        Log("Error:  " & Job.ErrorMessage)
        ToastMessageShow("Error:  " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Please note that you need the sub GetFilename from here too!
 
Last edited:

ibra939

Active Member
Licensed User
Longtime User
B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success )
  If Job.Success Then

        ' You are missing these lines....
        Dim JobTag As JobTag = job.Tag
         Dim dldta As DownloadData=JobTag.Data

   
        Dim OutStream As OutputStream
      Log("DownloadReady: "&dldta.url )
      ' -> DownloadReady: http://moysar.com/download/molay.mp3

      ' Please note the changes here too
      OutStream = File.OpenOutput(File.DirRootExternal,GetFilename(dldta.url),False) ' Getting filename with sub getfilename giving dldta.url to read the Original Filename we specify earlier in the creation of the Job

      File.Copy2(Job.GetInputStream,OutStream ) ' save the file
      Log(GetFilename(dldta.url)&" written to DirRootExternal")
      ' -> molay.mp3 written to DirRootExternal
        OutStream.Close
      ' The file should now be accessible in File.DirRootExternal
      ' Play the file now for ex.
     Else
        Log("Error:  " & Job.ErrorMessage)
        ToastMessageShow("Error:  " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Please note that you need the sub GetFilename from here too!


Test program ....... Thanks DonManfred o_O and checking GetFilename
 

cambopad

Active Member
Licensed User
Longtime User
How can I have more than two progress bars to track the progress of each downloading file?
 

aidymp

Well-Known Member
Licensed User
Longtime User
How can I have more than two progress bars to track the progress of each downloading file?

With 2 event names and 2 progress subs!

B4X:
Sub Button1_Click
Dim download1 As DownloadData
download1.url = "http://file1.com"
download1.EventName = "download1"
download1.Target = Me
end sub

Sub Button2_Click
Dim download2 As DownloadData
download2.url = "http://file1.com"
download2.EventName = "download2"
download2.Target = Me
end sub

Sub download1_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 download2_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
 

cambopad

Active Member
Licensed User
Longtime User
With 2 event names and 2 progress subs!

B4X:
Sub Button1_Click
Dim download1 As DownloadData
download1.url = "http://file1.com"
download1.EventName = "download1"
download1.Target = Me
end sub

Sub Button2_Click
Dim download2 As DownloadData
download2.url = "http://file1.com"
download2.EventName = "download2"
download2.Target = Me
end sub

Sub download1_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 download2_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
Thanks you for this suggestion, but How can I do if I want to download a lot of files without knowing about the number of downloads (I can say the number of progress subs) in advance?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Do you want to download all of these files at once?

You can create a class instance for each download that will handle the event (set it with the Target parameter). However you are probably making it more complicated than it should be.

You can decide to download up to 3 files at once and then start the next download each time the previous one completes. The overall download time will most probably be the same.
 

cambopad

Active Member
Licensed User
Longtime User
Do you want to download all of these files at once?

You can create a class instance for each download that will handle the event (set it with the Target parameter). However you are probably making it more complicated than it should be.

You can decide to download up to 3 files at once and then start the next download each time the previous one completes. The overall download time will most probably be the same.
Thank @Erel Yes, I want to download all of these files at once with their own progress bar! How to do that?
 

cambopad

Active Member
Licensed User
Longtime User
Create a class that holds a ProgressBar and use it to handle the events. Now you can create an instance of this class for each download.
For this method, do we still need to write many progress subs by hand? For example,
sub download2_Progress, sub
download3_Progress,...?
 

zeuspower

Member
Licensed User
Longtime User
Works OK with me,but with one problem,I don't get Total filesize

Probably because my file is on my Google Drive and the link is HTTPS ...
any suggestions ?
 
Status
Not open for further replies.
Top