I want to download 2 files in a row whilst showing progress i.e. a combination of #142968 and #126797 posts. So I have code of:
In post #142968, it states that you need add a TaskToJob.Size test in DownloadAndTrackProgress sub:
What should I change given that there is a test for "If HttpUtils2Service.TaskIdToJob.IsInitialized = False" in the FindTaskId sub?
B4X:
Private Sub B4XPage_Appear
Dim WebServer As String = "https://johnrose.mywire.org"
Dim TextHTML As String = "httpsLargeFile"
Dim TextFileName As String = "httpsLargeFile.txt"
Dim ImageHTML As String = "summer"
Dim ImageFileName As String = "summer.jpg"
FileName = TextFileName
ServerPath = WebServer & "/" & TextHTML
'In B4A you need to add to the Manifest
'SetApplicationAttribute(android:usesCleartextTraffic, "true")
Wait For (DownloadAndTrackProgress(ServerPath)) Complete (Success As Boolean)
If Success Then
Log("File was downloaded successfully")
Else
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
FileName = ImageFileName
ServerPath = WebServer & "/" & ImageHTML
Wait For (DownloadAndTrackProgress(ServerPath)) Complete (Success As Boolean)
If Success Then
Log("Image was downloaded successfully")
Else
Log("Image was NOT downloaded successfully")
xui.MsgboxAsync("Image was NOT downloaded successfully", Title)
Wait For MsgBox_Result (Result As Int)
B4XPages.ClosePage(Me)
End If
End Sub
Private Sub DownloadAndTrackProgress (url As String) As ResumableSub
LabelFileStatus.Text = "Starting"
Log("ServerPath: " & ServerPath)
Dim j As HttpJob
j.Initialize("", Me)
Log("https Initialized")
j.Download(url)
Wait For (FindTaskId(j)) Complete (TaskId As Int)
Dim b() As Boolean = Array As Boolean(False)
TrackProgress(j, b, TaskId)
Wait For (j) JobDone (j As HttpJob)
b(0) = True
If j.Success Then
Dim out As OutputStream = File.OpenOutput(ISFolder, FileName, False)
File.Copy2(j. GetInputStream, out)
out.Close '<------ very important
End If
j.Release
Log("File Downloaded")
Return j.Success
End Sub
Private Sub FindTaskId (job As HttpJob) As ResumableSub
Do While True
Sleep(30)
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
If HttpUtils2Service.TaskIdToJob.IsInitialized = False Then Continue
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
For Each id As Int In TaskToJob.Keys
If TaskToJob.Get(id) = job Then
Return id
End If
Next
Loop
Return -1
End Sub
Private Sub TrackProgress (j As HttpJob, Stop() As Boolean, TaskId As Int)
Do While Stop(0) = False
If j.Out.IsInitialized Then
FileSize = j.Response.ContentLength
Dim size As Long = File.Size(HttpUtils2Service.TempFolder, TaskId)
Log(size & ", " & FileSize)
End If
Sleep(100)
Loop
End Sub
In post #142968, it states that you need add a TaskToJob.Size test in DownloadAndTrackProgress sub:
B4X:
Private Sub DownloadAndTrackProgress (url As String, sFile As String) As ResumableSub
......
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False or TaskToJob.Size = 0
Sleep(30)
Loop
......
End Sub
What should I change given that there is a test for "If HttpUtils2Service.TaskIdToJob.IsInitialized = False" in the FindTaskId sub?