Android Question B4XPages download from https url with progress bar and wait for for it to finsh

johnaaronrose

Active Member
Licensed User
Longtime User
I'm using B4XPages to develop for Android using B4A. I want to download a SQLite database of just a few MB from a https url and wait for it to finish. I'm using the example code in old thread https://www.b4x.com/android/forum/t...from-server-with-progress.126797/#post-793290 coded in a module called by ShowPage from B4XMainPage.
My code looks exactly the same as the code in that thread (with a few lines added for Log statements etc) but I get a Compiler error of "Unknown member taskidtojob on
line 41 "Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob" in "Sub DownloadProgress" shown below:

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private SDDir As String
    Private DBFilename As String = "JewishWorld.db"
    Private pbDownload As ProgressBar
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("TestCode")
    B4XPages.SetTitle(Me, "TestCode")
        
    SDDir = xui.DefaultFolder
    Wait For(DownloadAndTrackProgress("https://app.box.com/s/vk2t0zrqaf0y16fk6evxnxcer13jb3ng")) Complete (Success As Boolean)
    If Success = True Then
        Private TempFullFilename As String
        TempFullFilename=HttpUtils2Service.TempFolder & DBFilename
        Log("Downloaded into: " & TempFullFilename)
        ToastMessageShow("Downloaded into " & TempFullFilename, True)
    Else
        Dim dbProblem As String
        dbProblem = "Database Not Available from Internet:" & CRLF & _
                             "Try Again Later."
        Wait For (xui.MsgboxAsync(dbProblem, "Database Problem")) Msgbox_Result (Result As Int)
        B4XPages.ClosePage(Me)
    End If
End Sub

Private Sub DownloadAndTrackProgress (url As String) As ResumableSub
    Log("DownloadAndTrackProgress")
    Private 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")
    Private 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
    Private 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
            Private TotalLength As Long = j.Response.ContentLength
            Private 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

PS I have HU_2 in the Conditional Symbols for Build Configuration and I have library OkHttpUtils2 included.
 

Lucas Siqueira

Active Member
Licensed User
Longtime User
I'm using B4XPages to develop for Android using B4A. I want to download a SQLite database of just a few MB from a https url and wait for it to finish. I'm using the example code in old thread https://www.b4x.com/android/forum/t...from-server-with-progress.126797/#post-793290 coded in a module called by ShowPage from B4XMainPage.
My code looks exactly the same as the code in that thread (with a few lines added for Log statements etc) but I get a Compiler error of "Unknown member taskidtojob on
line 41 "Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob" in "Sub DownloadProgress" shown below:

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private SDDir As String
    Private DBFilename As String = "JewishWorld.db"
    Private pbDownload As ProgressBar
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("TestCode")
    B4XPages.SetTitle(Me, "TestCode")
     
    SDDir = xui.DefaultFolder
    Wait For(DownloadAndTrackProgress("https://app.box.com/s/vk2t0zrqaf0y16fk6evxnxcer13jb3ng")) Complete (Success As Boolean)
    If Success = True Then
        Private TempFullFilename As String
        TempFullFilename=HttpUtils2Service.TempFolder & DBFilename
        Log("Downloaded into: " & TempFullFilename)
        ToastMessageShow("Downloaded into " & TempFullFilename, True)
    Else
        Dim dbProblem As String
        dbProblem = "Database Not Available from Internet:" & CRLF & _
                             "Try Again Later."
        Wait For (xui.MsgboxAsync(dbProblem, "Database Problem")) Msgbox_Result (Result As Int)
        B4XPages.ClosePage(Me)
    End If
End Sub

Private Sub DownloadAndTrackProgress (url As String) As ResumableSub
    Log("DownloadAndTrackProgress")
    Private 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")
    Private TaskId As Int
    Do While TaskId = 0
        Para cada id como Int em TaskToJob.Keys
            If TaskToJob.Get(id) = j Then
                ID da tarefa = ID
                Log("TaskId="&id)
                Saída
            Fim se
        Próximo
        Dormir(10)
    Laço
    Private b() As Boolean = Array As Boolean(False)
    TrackProgress(j, b, TaskId)
    Aguarde (j) JobDone (j As HttpJob)
    b(0) = Verdadeiro
    j. Lançamento
    Log("Banco de dados baixado")
    Retornar j.Sucesso
Finalizar Sub

Private Sub TrackProgress (j As HttpJob, Stop() As Boolean, TaskId As Int)
    Log("Acompanhar o Progresso")
    Faça Enquanto Parar(0) = False
        Se j.Out.IsInitialized Then
            Private TotalLength As Long = j.Response.ContentLength
            Tamanho privado As Long = File.Size(HttpUtils2Service.TempFolder, TaskId)
            Log(tamanho & " de " & TotalLength & " baixado")
            pbDownload.Progress=Round((tamanho*100)/TotalLength)
        Fim se
        Dormir(100)
    Laço
    pbDownload.Progress=Round((tamanho*100)/TotalLength)
    Log("Banco de dados: baixado: " & tamanho & " deve ser igual a " & TotalLength)
Finalizar Sub
[/CÓDIGO]

PS Eu tenho HU_2 nos símbolos condicionais para configuração de compilação e tenho a biblioteca OkHttpUtils2 incluída.
[/QUOTE]
you need the link to the file, not the link to the page that has the button to download the file...

no use https://app.box.com/s/vk2t0zrqaf0y16fk6evxnxcer13jb3ng

use https://dl3.boxcloud.com/d/1/a1!xm0jIHvA1_X_nX-zLLWwPVoyvkR_Sf8MkwRiz97kzJ9KMjj24ujw33JmT6rlciCqnAebvQXhgUYjNYliB4outyXwd-H1WffAWeECE_muu1ojsGs4F3P1xJ0oqzv1DGL9tfAV_wFxBpMr0SNMNuY4nip_qlvHwUwe5TxEcr0ex0nlIanP0_4Q7AsXQz7-jXzbmuQdlgnp7x648-AWDFYTGU6nWkvQf5iV3u7S7_60N_UVeI1WS5tYiWdbDk4HCcsYJlzo9GrcC3BuZspdOnU2ivnBxz38CFTUoUePjEBC5hYzald1B5LSFYbi6x11wpHCLPcuWbzHdP3wZnUWJMlEK3I4oNzeLejuS3YLZ1v_GUXeW692L47ywk1RpDUG_9mGYvOb7F_29ckgXWMR_3GOl8u-ch-13lMGWd9Gt3-_otaAVGRiRKBQR7iqqa0rxQj_iQzVeK-L8e9gTchJT7G4uceLQtKPXkpV4z4w7ZihP5uhKMc5jcnSbLXuIsIathB2DCifFjuSZop-FZPIqf4esJCeh6ulGv-JdITxpOG2J__86Lg6-8Jg7cR_o-qHyPBXviZc3a4bXCVKCJ2K7AlzKuK5Mor1QqLj2dCZ-PVB99DIxb0UbaqF3cDr9Rs8FmMw594FCeHCn-Znwli3qt1y2YB8ib06TBw3RbnnjcXehNryShwfgkD4dChH0ZnVNW9FeEMq_-luHrxEUgTuBD7SmUJPk5041JSCJsa3xnXmed2Z4VIXtxpDkgtARpp8wkY-ACJVi51dAPaIK4X6gVLYGCN_mm7D9mOoGIydMitV7lDEeWtDqskQQcMesQ8Pay0VrPdOINEIkD-GkmJThisTVQrMjkAuaKIbj2rWjw5Sb3ZvwUyATV0ARP5u1TFcVF5xnxOjJF232FqkqBsKRWKzfeX_df2v76DNo8XzpTErLrkZAC6mn1_hri8YLuTdR7V_eDo0wpib1Q2Nap5-e6Uwt9l49ic9xLZ5eP_8MUulOrO1fRz036Ij81K0sGULtDicPO6_3EZOMxM4VsqvA2VuT6trzS2AHBvKTdCRB6dArXeJ68TUTomKNj5DunFdqzqGEJlZBo5oTT2AeaT1aBMmJC9dO0Uq5h7LBA674Pz7zXznCTPfyS76pJg56Mz49MuKsrvPHtpWoE8BYRNg0IY5SIA6xxcYj9s5wNuopJJAnkZbAnSEmAjXUzKzv9XE89QaKc4VCcMuxT4V8-A1-7nInCXDeFWDl6PNFJTHnHQj4iR02cKi5p3IuRswTF279-VZIDRpQdicBzwRmABotz9OpdgpnUFh2GcignBiEN0sVtPaGpS0Q1ipmx6IpXsl/download


1644675222868.png
 
Upvote 1

johnaaronrose

Active Member
Licensed User
Longtime User
@Lucas Siqueira Thanks for that correction. I think that box.com used to automatically download after using the url shown for the relevant file in the browser display. However, I now see that there is a Download button.

However, my original query about the Compile error is still active.
 
Last edited:
Upvote 0
Top