Android Question HttpJob : wait for the end of downloading

voxel

Member
Licensed User
Hello,

I would like to stay on the "init" screen as long as the download is not complete and then display the "end" screen. With this code, there is no waiting. Thanks for your help.

B4X:
Activity.LoadLayout("init")
    
Dim job As HttpJob
job.Initialize("j", Me)
    
job.Download("https://test/test.txt")
Wait For (job) JobDone(j As HttpJob)
If j.Success Then
    Activity.LoadLayout("end")
Else
    Activity.LoadLayout("error")
End If
j.Release
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim Success As Boolean
    
    Dim job As HttpJob
    job.Initialize("j", Me)
    
    job.Download("https://test/test.txt")
    Wait For (job) JobDone(j As HttpJob)
    LogColor("After http job", Colors.Blue)
    Success = j.Success
    j.Release
    
    If Success Then
        Activity.LoadLayout("end")
        ActResumeCode
    Else
        Activity.LoadLayout("error")
    End If
End Sub

Sub Activity_Resume
    LogColor("Activity_Resume", Colors.Blue)
    ' Move all your code from here to ActResumeCode
End Sub
Sub ActResumeCode
    LogColor("ActResumeCode", Colors.Blue)
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Furthermore, the code I posted is not good, as a subsequent Resume would not execute the ActResumeCode.
A flag would be needed, a global variable.
This is better:
B4X:
Sub Process_Globals
    Private mResumeOnly As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim Success As Boolean
   
    Dim job As HttpJob
    job.Initialize("j", Me)
   
    job.Download("https://test/test.txt")
    Wait For (job) JobDone(j As HttpJob)
    LogColor("After http job", Colors.Blue)
    Success = j.Success
    j.Release
   
    If Success Then
        Activity.LoadLayout("end")
        ActResumeCode
    Else
        Activity.LoadLayout("error")
    End If
End Sub

Sub Activity_Resume
    LogColor("Activity_Resume", Colors.Blue)
    ' Move all your code from here to ActResumeCode
    If mResumeOnly Then
        ActResumeCode
    End If
End Sub
Sub ActResumeCode
    mResumeOnly = True
    ' Here your code
   
End Sub
 
Last edited:
Upvote 0
Top