I have used some code from a B4J answer on https://www.b4x.com/android/forum/threads/simple-progress-http-download.127214/#post-796463 on Feb 2, 2021 as the post said that it also worked for B4A. I've called the code from 2 places in my program: I don't know if that makes a difference. So my code has a call to Sub Download in 2 places:
Download contains:
After that Subs DownloadAndTrackProgress & TrackProgress are coded as listed in the B4J answer referred to at the start of this post, with the addition of some Log statements:
It seems that the Sub DownloadAndTrackProgress is called (as the Log shows "DownloadAndTrackProgress" & "Waiting for HttpUtils2Service to be ready.") but there is no match on TaskToJob.Get(id) = j (coded in Sub DownloadAndTrackProgress) so TrackProgress is never called.
Is there an error in the code for Sub DownloadAndTrackProgress?
B4X:
Sub Activity_Create(FirstTime As Boolean)
Download
B4X:
Sub btnDownload_Click
Download
End Sub
Download contains:
B4X:
Sub Download
Wait For(DownloadAndTrackProgress("https://app.box.com/s/vk2t0zrqaf0y16fk6evxnxcer13jb3ng")) Complete (Success As Boolean)
After that Subs DownloadAndTrackProgress & TrackProgress are coded as listed in the B4J answer referred to at the start of this post, with the addition of some Log statements:
B4X:
Private Sub DownloadAndTrackProgress (url As String) As ResumableSub
Log("DownloadAndTrackProgress")
Dim j As HttpJob
j.Initialize("", Me)
j.Download(url)
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False
Log("Waiting for HttpUtils2Service to be ready.")
Sleep(30)
Loop
Log("Checking Tasks")
Dim TaskId As Int
Do While TaskId = 0
For Each id As Int In TaskToJob.Keys
If TaskToJob.Get(id) = j Then
TaskId = id
Log("TaskId="&id)
Exit
End If
Next
Sleep(10)
Loop
Dim b() As Boolean = Array As Boolean(False)
TrackProgress(j, b, TaskId)
Wait For (j) JobDone (j As HttpJob)
b(0) = True
j.Release
Log("Database Downloaded")
Return j.Success
End Sub
Private Sub TrackProgress (j As HttpJob, Stop() As Boolean, TaskId As Int)
Log("Track Progress")
Do While Stop(0) = False
If j.Out.IsInitialized Then
Dim TotalLength As Long = j.Response.ContentLength
Dim size As Long = File.Size(HttpUtils2Service.TempFolder, TaskId)
Log(size & " of " & TotalLength & " downloaded")
pbDownload.Progress=Round((size*100)/TotalLength)
End If
Sleep(100)
Loop
pbDownload.Progress=Round((size*100)/TotalLength)
Log("Database: Downloaded: " & size & " should equal " & TotalLength)
End Sub
It seems that the Sub DownloadAndTrackProgress is called (as the Log shows "DownloadAndTrackProgress" & "Waiting for HttpUtils2Service to be ready.") but there is no match on TaskToJob.Get(id) = j (coded in Sub DownloadAndTrackProgress) so TrackProgress is never called.
Is there an error in the code for Sub DownloadAndTrackProgress?