Android Question How to show Progress on https Download

johnaaronrose

Active Member
Licensed User
Longtime User
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:
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?
 

johnaaronrose

Active Member
Licensed User
Longtime User
@Erel The file is only 2MB. Do you define that as large?
I want the Main Activity to wait for the completion of the https download, which is why I'm using "Wait For". I also, want the progress of the download to be shown on the pbDownload progress bar (defined in the layout used by the Main Activity) as it is downloading.
I've never used B4XPages or coded a Service Module. I've had a brief look at the documentation for B4XPages in the online manual. I've looked at the online documentation for B4XPages and Service Modules. I'm only interested in developing for Android. Which would you recommend? Are there any online tutorials for your recommendation?
 
Last edited:
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
@Erel
I want the Main Activity to wait for the completion of the https download, which is why I'm using "Wait For". I also, want the progress of the download to be shown on the pbDownload progress bar (defined in the layout used by the Main Activity) as it is downloading.
I've never used B4XPages or coded a Service Module. I've had a brief look at the documentation for B4XPages in the online manual. I've looked at the online documentation for B4XPages and Service Modules. I'm only interested in developing for Android. Which would you recommend? Are there any online tutorials for your recommendation?
PS Given that 2MB is not a large download, I don't understand why the download fails with the coding previously shown.
 
Upvote 0
Top