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:

Giacomo

Active Member
Licensed User
Longtime User
I try to compile this app " LargeFileDownload" but I always make the same mistake.
I already tried to reinstall B4A without result. ( and erase all Lib)


upload_2016-2-7_15-46-0.png
 

Alisson

Active Member
Licensed User
I have one url with file. Exemple:
http://www.site.com/rss/
How get the file in url and save to scard/

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals
    Private link1 As String = "http://www.site.com/rss/"
End Sub

Sub Globals
    Dim ProgressBar1 As ProgressBar
    Dim Label1 As Label
    Dim btnDownload As Button
    Dim btnCancel As Button
End Sub

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

Sub btnDownload_Click
    Dim dd As DownloadData
    dd.url = link1
    dd.EventName = "dd"
    dd.Target = Me
    CallSubDelayed2(DownloadService, "StartDownload", dd)
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)
    Dim o As OutputStream
    o = File.OpenOutput(File.DirDefaultExternal, "file.xml", True)
    'File.Copy(File.DirRootExternal & "/Android","photo1.jpg",File.DirRootExternal & "","photo.jpg")
    File.Copy2(Job.GetInputStream, o)
    o.Close
    Job.Release
End Sub

Sub btnCancel_Click
    CallSubDelayed2(DownloadService, "CancelDownload", link1)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Thanks
 

Alisson

Active Member
Licensed User
Solvetion:

B4X:
Log("Job completed: " & Job.Success)
     Job.Download("http://www.site.com/rss/")
           Dim out As  OutputStream
          out = File.OpenOutput(File.DirRootExternal,"file.xml",False)
           File.Copy2(Job.GetInputStream,out)
           out.Close
    Job.Release

Thanks
 

Alisson

Active Member
Licensed User
I change the code to:


B4X:
Dim job3 As HttpJob
        job3.Initialize("Job3", Me)
   job3.Download(link1)   
    End If
  
End Sub
Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
       Job.Download(link1)
           Dim out As  OutputStream
          out = File.OpenOutput(File.DirRootExternal,"folder/"&s&".xml",False)
           File.Copy2(Job.GetInputStream,out)
           out.Close
   End If
   Job.Release
End Sub

Is correct ?
 

DonManfred

Expert
Licensed User
Longtime User
Is correct ?
no. Sorry, i did not see your error before

1. The download Job start

B4X:
   Dim job3 As HttpJob
   job3.Initialize("Job3", Me)
   job3.Tag = s ' to be used in jobdone. Place here the number you want to have the resulting xml named with
   job3.Download(link1)

B4X:
Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
      if job.jobname = "Job3" then
         Dim out As  OutputStream
         out = File.OpenOutput(File.DirRootExternal,"folder/"&job.tag&".xml",False)
         File.Copy2(Job.GetInputStream,out)
         out.Close
      end if
   End If
   Job.Release
End Sub
 

jazzzzzzz

Active Member
Licensed User
Longtime User
I have okhttputilis2 linked in my project,will that conflicts to this lib? If yes how can I get download progress?
 

jazzzzzzz

Active Member
Licensed User
Longtime User
Ok I will try..

Am using imagedownloder class for downloading now and is it possible to add this download progress there also?
 

Tempomaster

Active Member
Licensed User
Longtime User
Hello,

the example works very well with small files. In my app you can download a large file (~42 Mb):

http://www.g-daehling.de/tempomaster/test.zip

It is very often several attempts to load the file completely. The criticism comes from very many users of the app. What can be the problem ?

Best regards,
Gunnar from Magdeburg in Germany
 
Status
Not open for further replies.
Top