Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private Title As String ="HTTPS"
Private LabelTrackDownload As Label
Private ProgressBarDownload As ProgressBar
Private FileName As String = "HTTPSLargeFile.txt"
Private ASFolder As String
Private FileSize As Long
End Sub
'You can add more parameters here.
Public Sub Initialize As Object
Return Me
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("https")
B4XPages.SetTitle(Me, Title)
Dim rp As RuntimePermissions
ASFolder = rp.GetSafeDirDefaultExternal("")
Log("Accessible Storage Folder: " & ASFolder)
If File.Exists(ASFolder, FileName) Then
Log(FileName & " File Exists in Accessible Storage Folder " & ASFolder)
Log("Deleting " & FileName & " from Accessible Storage Folder " & ASFolder)
File.Delete(ASFolder, FileName)
End If
End Sub
Private Sub B4XPage_Appear
LabelTrackDownload.Text = "Not Started"
ProgressBarDownload.Progress = 0
Wait For (DownloadAndTrackProgress) Complete (Success As Boolean)
If Success Then
Log("File was copied successfully")
Else
Log("File was not copied")
End If
End Sub
Private Sub DownloadAndTrackProgress As ResumableSub
Dim WebServer As String = "https://johnrose.mywire.org"
Dim WebDirectory As String = "Test"
Dim ServerPath As String = WebServer & "/" & WebDirectory & "/" & FileName
Log("ServerPath: " & ServerPath)
Dim HTTPS1 As HttpJob
HTTPS1.Initialize("", Me)
Log("HTTPS Initialize Started")
'For a non-https link, need to add SetApplicationAttribute(android:usesCleartextTraffic, "true") in B4A.
HTTPS1.Download(ServerPath)
Log("Started HTTPS")
Sleep(0)
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False
Sleep(30)
Loop
Log("HTTPS Initialize Finished")
Dim TaskId As Int
For Each Id As Int In TaskToJob.Keys
If TaskToJob.Get(Id) = HTTPS1 Then
TaskId = Id
Exit
End If
Next
Dim JobFinished As Boolean = False
TrackProgress(HTTPS1, JobFinished, TaskId)
Log("Wait For Start")
Wait For (HTTPS1) JobDone (HTTPS1 As HttpJob)
Log("Wait End")
JobFinished = True
If JobFinished = True Then
Log("Job Finished")
Else
Log("Job Not Finished")
End If
If HTTPS1.Success Then
Log(HTTPS1.GetString)
Log("File was downloaded successfully")
Dim out As OutputStream = File.OpenOutput(File.DirInternal, FileName, False)
File.Copy2(HTTPS1.GetInputStream, out)
out
Logger connected to: Blackview A80Pro
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
*** mainpage: B4XPage_Created
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
*** https page: B4XPage_Created [mainpage]
Accessible Storage Folder: /storage/emulated/0/Android/data/org.mywire.johnrose.httpprogresssexample/files
*** mainpage: B4XPage_Disappear [mainpage]
*** https page: B4XPage_Appear [mainpage]
ServerPath: https://johnrose.mywire.org/Test/HTTPSLargeFile.txt
HTTPS Initialize Started
Started HTTPS
*** Service (httputils2service) Create ***
(Http client initialized with accept all option.)
** Service (httputils2service) Start **
HTTPS Initialize Finished
Start of TrackProgress
Track Job Not Finished
Wait For Start
Wait End
Job Finished
** First few lines of Large Text file **
Message longer than Log limit (4000). Message was truncated.
File was downloaded successfully
Accessible Storage File Size = 0KB
File was copied successfully
i=1000
i=2000
i=3000
.Close '<------ very important
Else
Log(HTTPS1.GetString)
Log("File was NOT downloaded successfully")
xui.MsgboxAsync("File was NOT downloaded successfully", Title)
Wait For MsgBox_Result (Result As Int)
B4XPages.ClosePage(Me)
End If
HTTPS1.Release
FileSize = File.Size(ASFolder, FileName)
Log("Accessible Storage File Size = " & Round(FileSize / 1000) & "KB")
ProgressBarDownload.Progress = 100
Sleep(0)
Return HTTPS1.Success
End Sub
Sub TrackProgress (HTTPS1 As HttpJob, JobFinished As Boolean, TaskId As Int)
Log("Start of TrackProgress")
Dim i As Int = 0
Dim j As Int = 0
If JobFinished = True Then
Log("Track Job Finished")
Else
Log("Track Job Not Finished")
End If
Do While JobFinished = False
If HTTPS1.Out.IsInitialized Then
i = i + 1
j = Floor(i/1000)*1000
If i = j Then
Log("i="&i)
End If
Dim TotalSize As Long = HTTPS1.Response.ContentLength
Dim DownloadedSize As Long = File.Size(HttpUtils2Service.TempFolder, TaskId)
LabelTrackDownload.Text = NumberFormat(DownloadedSize, 0, 0) & " of " & NumberFormat(TotalSize, 0, 0)
ProgressBarDownload.Progress = (DownloadedSize*100)/TotalSize
End If
Sleep(100)
Loop
Log("End of TrackProgress")
End Sub