Android Question Two files to be downloaded with progress

johnaaronrose

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

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?
 
Solution
1.Don't use to wait for DownloadAndTrackProgress
2.Move taskid to Sub DownloadAndTrackProgress
B4X:
Private Sub B4XPage_Appear
...
DownloadAndTrackProgress(ServerPath, TextFileName)
DownloadAndTrackProgress(ServerPath, ImageFileName)
...
End Sub
Private Sub DownloadAndTrackProgress (url As String, sFile As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(url)
  
    Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
    Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False Or TaskToJob.Size = 0
        Sleep(30)
    Loop
      
    Dim TaskId As Int
    For Each id As Int In TaskToJob.Keys
        If TaskToJob.Get(id) = j Then
            TaskId = id
            Exit
        End If
    Next...

teddybear

Well-Known Member
Licensed User
1.Don't use to wait for DownloadAndTrackProgress
2.Move taskid to Sub DownloadAndTrackProgress
B4X:
Private Sub B4XPage_Appear
...
DownloadAndTrackProgress(ServerPath, TextFileName)
DownloadAndTrackProgress(ServerPath, ImageFileName)
...
End Sub
Private Sub DownloadAndTrackProgress (url As String, sFile As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(url)
  
    Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
    Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False Or TaskToJob.Size = 0
        Sleep(30)
    Loop
      
    Dim TaskId As Int
    For Each id As Int In TaskToJob.Keys
        If TaskToJob.Get(id) = j Then
            TaskId = id
            Exit
        End If
    Next
    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(File.DirTemp, sFile, False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
    End If
  
    j.Release
    Return j.Success
End Sub
Private Sub TrackProgress (j As HttpJob, Stop() As Boolean, TaskId As Int)
    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("taskid="&TaskId&" "&size & ", " & TotalLength)
        End If
        Sleep(100)
    Loop
End Sub
 
Upvote 0
Solution

johnaaronrose

Active Member
Licensed User
Longtime User
Not yet tried above solution as I thought I'd try solution for downloading two files without progress being displayed: not sure about that as being suitable for B4A as it was in B4J sub-topic. So I have:

[CODE lang="b4x" title="Private
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
Wait For (Download(ServerPath)) Complete (Success As Boolean)
If Success Then
FileSize = File.Size(ISFolder, FileName)
Log(NumberFormat(FileSize, 0, 0) & " of " & NumberFormat(FileSize, 0, 0))
Log("File was downloaded successfully")
LabelFileStatus.Text = "Download of File Completed"
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 (Download(ServerPath)) Complete (Success As Boolean)
If Success Then
FileSize = File.Size(ISFolder, FileName)
Log(NumberFormat(FileSize, 0, 0) & " of " & NumberFormat(FileSize, 0, 0))
Log("Image was downloaded successfully")
LabelImageStatus.Text = "Download of Image Completed"
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 Download (url As String) As ResumableSub
...
Dim j As HttpJob
j.Initialize("", Me)
j.Download(url)
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False Or TaskToJob.Size = 0
Sleep(30)
Loop
Wait For (j) JobDone (j As HttpJob)
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
Return j.Success
End Sub[/CODE]

However, that code results in the app hanging after downloading the first (text) file and during the download of the second (image) file.
 
Upvote 0
Top