B4J Question Simple progress > http.download

TheRealMatze

Active Member
Licensed User
Hi,
is there a simple way to get the download-progress on all platforms? I use "jOKHttpUtils2" 2.95

B4X:
Private http As HttpJob
    http.Initialize("",Me)
    
    http.Download("https://url.url/file.ext")
    http.GetRequest.Timeout = 10000
    Log(http.Response.StatusCode)
    
    Wait For (http) JobDone(http As HttpJob)
    If http.Success Then
        Log("Complete!")
    else
        Log("Nope")
    end if
    
    ...

The downloads are not extrem large, about 10megs, but on mobile data it will be took longer maybe. It would be enough if i know the incomming size, i can provide the full size on the server.

Regards
Matthias
 

TheRealMatze

Active Member
Licensed User
Hey Erel,
i´ve read the whole article, but it did not work in b4a. In b4j i got the correct size and length of the download, in b4a the length is correct - but the size is always 0 (like Dadaista in the thread you refer to). The Download itself works on both plattforms...

Regards
Matthias
 
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
but the size is always 0

Try this below
B4X:
Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(url)
   
    Sleep(0)    '<------ THIS!!!!
   
    Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
    Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False
        Sleep(30)
    Loop
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If the server supports range requests then this is the best solution: https://www.b4x.com/android/forum/threads/b4x-rangedownloader-resumable-downloads.133370/

This code works in both B4A and B4J:

B4X:
Private Sub Button1_Click
    'non-https link so we need to add SetApplicationAttribute(android:usesCleartextTraffic, "true") in B4A.
    Wait For (DownloadAndTrackProgress("http://mirror.filearena.net/pub/speed/SpeedTest_16MB.dat")) Complete (Success As Boolean)
End Sub

Private Sub DownloadAndTrackProgress (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
        Log("Waiting for HttpUtils2Service to be ready.")
        Sleep(30)
    Loop
    Dim TaskId As Int
    Do While TaskId = 0
        For Each id As Int In TaskToJob.Keys
            If TaskToJob.Get(id) = j Then
                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("complete")
    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(size & ", " & TotalLength)
        End If
        Sleep(100)
    Loop
    Log(size & ", " & TotalLength)
End Sub

1. Don't forget to add HU2_PUBLIC to the build configuration.
2. I've tested it on a version where the bug discussed above is fixed. The bug happens in debug mode. Start with testing it in release mode.
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Welcome to the forum @TheRealMatz.

Anyway here you go, here is an example on how to download a file which includes a download progress bar.
1612267208501.png



Enjoy...
 

Attachments

  • DownloadProgressBar.zip
    3.2 KB · Views: 645
Last edited:
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
Line 10 in code (from Feb 2, 2021 post) gives "Unknown member: taskidtojob" error:
B4X:
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
That's when I have OkHttputils library included. If I include OkHttpUtils2 library, I get "HttpUtils2Service is declared twice. You should either remove the library reference or the code module.".
What should I do?
 
Upvote 0

hayk

Member
If the server supports range requests then this is the best solution: https://www.b4x.com/android/forum/threads/b4x-rangedownloader-resumable-downloads.133370/
This code works in both B4A and B4J:

B4X:
Private Sub Button1_Click
    'non-https link so we need to add SetApplicationAttribute(android:usesCleartextTraffic, "true") in B4A.
    Wait For (DownloadAndTrackProgress("http://mirror.filearena.net/pub/speed/SpeedTest_16MB.dat")) Complete (Success As Boolean)
End Sub

Private Sub DownloadAndTrackProgress (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
        Log("Waiting for HttpUtils2Service to be ready.")
        Sleep(30)
    Loop
    Dim TaskId As Int
    Do While TaskId = 0
        For Each id As Int In TaskToJob.Keys
            If TaskToJob.Get(id) = j Then
                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("complete")
    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(size & ", " & TotalLength)
        End If
        Sleep(100)
    Loop
    Log(size & ", " & TotalLength)
End Sub

1. Don't forget to add HU2_PUBLIC to the build configuration.
2. I've tested it on a version where the bug discussed above is fixed. The bug happens in debug mode. Start with testing it in release mode.
how to make it for B4A?
 
Upvote 0
Top