Android Tutorial Download huge files with HttpUtils2

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
It is not related to https. The total size is fetched from the content length header.

I guess that in this case the server doesn't return the correct value in this header.
 

Ricardo Bunschoten

Member
Licensed 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

Administrator
Staff member
Licensed 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

Member
Licensed 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:

Erel

Administrator
Staff member
Licensed User
The downloads folder is File.Combine(File.DirRootExternal, "Download").

Copying the file to this folder is simple. If you wan to save it directly to this folder then you will need to understand how HttpUtils2 works and modify it (or use HttpClient directly).
 

Ricardo Bunschoten

Member
Licensed 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
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

Member
Licensed 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

Member
Licensed 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
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
 

Mashiane

Expert
Licensed User
Hi I cant compile this app, its giving me an error when I try to run

B4X:
src\b4a\example\httputils2service.java:144: error: cannot access ParseException
anywheresoftware.b4a.keywords.Common.Log(_response.GetString("UTF8"));
                                                            ^
  class file for org.apache.http.ParseException not found
 

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
 
Status
Not open for further replies.
Top