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

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
If i want to write the download file to a folder like the default folder Download in the internal or external memory ? is that possible instead of to the android data/app dir ?

And if so whats the exact code then ?

I have now this code and with the help of this topic it downloads the zipfile and saves it in the android data/application name folder

But i want it to be in the default Download dir of the internal memory or on a sd card and then in a specified folder

Second if i download like a apk file with this script is it also possible in B4a to execute that downloaded apk so it's been installed ?

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://download.thinkbroadband.com/20MB.zip"
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, "20mb.zip", False)
    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
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Second if i download like a apk file with this script is it also possible in B4a to execute that downloaded apk so it's been installed ?
It is possible and there are several threads that explain how to do it. Start a new thread if you don't find them.

Your code will copy the file to DirDefaultExternal. What exactly do you want to change?
 

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
Thnx for the reply.

The code copiess/saves the file to android/data/applicationmamefolder/files/

I want the downloaded file to be saved somewhere else.

Android has a default folder named Download

I want the file to be saved in that Downloadfolder.

Standard that folder is located in internal sdcard/Download
 
Last edited:

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
OK i managed to change the code so that the file is beeing downloaded to the internal folder Download
Not like you said but this also works

B4X:
#Region  Project Attributes
    #ApplicationLabel: Veolo 4K Kodi Skin installer
    #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 = "https://copy.com/6R9j8KkEkocJTU3k/WINDOWS+XP.pdf"
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.dirrootExternal, "Download/Windowsxp.pdf", False)
    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

The part
B4X:
     Dim o As OutputStream
    o = File.OpenOutput(File.dirrootExternal, "Download/Windowsxp.pdf", False)
    File.Copy2(Job.GetInputStream, o)
    o.Close
    Job.Release
End Sub
Saves it to the default download folder.
 

fixit30

Active Member
Licensed User
Longtime User
You should really check that Job.Success = True before trying to save the file otherwise it could cause you issues if the job fails.
 

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
Question. I want to download files from a bunch of urls. These urls are specified in a txt file and this file located on a webserver

Example http://www.acryan.com/Veolo4kskin/url.txt
This file contains the filename and url

Is this possible ?

You can use the List1 = File.ReadList(File.DirAssets, "url.txt") option but then you have to put the txt file local and i want it online so i can change the urls without recompiling the app :)

is there a example maybe available how to to this ?
 

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
I know i have to parse it but can not find in the documentation how to do that ;)


I know use this
B4X:
Sub btnDownload_ClickDim dd As DownloadData
 dd.url = link1
 dd.EventName = "dd"
 dd.Target = Me
 CallSubDelayed2(DownloadService, "StartDownload", dd)

where link 1 is specified in Sub Process_GlobalsPrivate link1 As String = https://copy.com/f0m16GtdU2h7wIqz/test.zip
 

ibra939

Active Member
Licensed User
Longtime User
OK i managed to change the code so that the file is beeing downloaded to the internal folder Download
Not like you said but this also works

B4X:
#Region  Project Attributes
    #ApplicationLabel: Veolo 4K Kodi Skin installer
    #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 = "https://copy.com/6R9j8KkEkocJTU3k/WINDOWS+XP.pdf"
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.dirrootExternal, "Download/Windowsxp.pdf", False)
    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

The part
B4X:
     Dim o As OutputStream
    o = File.OpenOutput(File.dirrootExternal, "Download/Windowsxp.pdf", False)
    File.Copy2(Job.GetInputStream, o)
    o.Close
    Job.Release
End Sub
Saves it to the default download folder.



This is working if i make URL for any thing ? any one test it
 

ArminKH

Well-Known Member
the class is using the "old" httputils instead of the new provided with okHTTP?
Or you are using android 6? In this case you need to update your httputils with the okHTTP version of them
i get above error too
i'm using android 23 to compile my app and i'm failed but there is not any problem when i'm using android 22
as you said the problem is when i want to use it in android 6
so as this is a class how is possible to change themes based on ok http?
which part of code should be changed and what is the ok http code for that point?
thank u
 

ArminKH

Well-Known Member
maybe @Erel need to update this class (or add another version) to support OkHttp
 
Status
Not open for further replies.
Top