Android Question Download Progress for HTTPS

johnaaronrose

Active Member
Licensed User
Longtime User
I tried to get progress displaying on a HTTPS download using the thread at https://www.b4x.com/android/forum/threads/how-to-show-progress-on-https-download.138174
That thread's coding seemed very convoluted to me and Erel advised me to convert the app to B4XPages. I did that and had problems (which were resolved) as shown in /https://www.b4x.com/android/forum/t...ress-bar-and-wait-for-for-it-to-finsh.138365/
I have HU2_PUBLIC in the Conditional Symbols for Build Configuration and I have library OkHttpUtils2 included.
However, I'm still not able to get the progress display working. I would like to get rid of the convoluted code (from the first thread) and use the simpler code similar to that for a FTP download:
B4X:
Dim FTP1 As FTP
    FTP1.Initialize("FTP", "rose.myddns.me", 21, "john", "Rose33126")
    FTP1.PassiveMode = True
    FTP1.UseSSL = True
    Log("Started FTP")
    Sleep(0)
    Dim sf As Object = FTP1.DownloadFile(ServerPath, True, File.DirAssets, FileName)
    Wait For (sf) FTP1_DownloadCompleted (ServerPath As String, Success As Boolean)
    If Success Then .....
    
Private Sub FTP_DownloadProgress(ServerPath As String, TotalDownLoaded As Long, Total As Long)
    Log("Downloaded: " & Round(TotalDownLoaded / 1000) & "kb")
    ProgressBarDownload.Progress=TotalDownLoaded*100/Total
    Sleep(100)
End Sub
There may be errors in the above code as I haven't yet fully written the FTP-using app let alone tested it.
Can an HTTPS download use similar code to monitor the progress of the HTTPS download?
PS what does HU2_PUBLIC in the Configuration mean & do?
 

DonManfred

Expert
Licensed User
Longtime User
You are confusing HTTP(s) (okhttputils2) and FTP?
FTP does not do any http(s)-Calls.
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
You are confusing HTTP(s) (okhttputils2) and FTP?
FTP does not do any http(s)-Calls.
I'm not confusing them. The reason I 'talked' about FTP was because I'm asking if a similar facility (i.e. using an event for Progress of an HTTP(S) can be used as for FTP.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
convoluted code
What I can see is that you just need to copy and paste two subs in your code:

B4X:
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
        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
    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(size & ", " & TotalLength)
        End If
        Sleep(100)
    Loop
End Sub

And then, when you want to download something use:

B4X:
 Wait For (DownloadAndTrackProgress("http://mirror.filearena.net/pub/speed/SpeedTest_16MB.dat")) Complete (Success As Boolean)

Ah, and add to the build configuration (Ctrl + B): HU2_PUBLIC
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
What I can see is that you just need to copy and paste two subs in your code:

B4X:
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
        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
    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(size & ", " & TotalLength)
        End If
        Sleep(100)
    Loop
End Sub

And then, when you want to download something use:

B4X:
 Wait For (DownloadAndTrackProgress("http://mirror.filearena.net/pub/speed/SpeedTest_16MB.dat")) Complete (Success As Boolean)

Ah, and add to the build configuration (Ctrl + B): HU2_PUBLIC
That is the convoluted code that I previously referred to. A simpler way would be to use an event such as FTP_DownloadProgress if such existed.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Both FTP and HTTPS are protocols - a set of rules. HTTP was initially introduced as a "lightweight" equivalent of FTP, intended to be used for short web-page elements rather than files. To quote from Wikipedia ...

"HTTP originally dropped the connection after each transfer because doing so was so cheap. While HTTP has subsequently gained the ability to reuse the TCP connection for multiple transfers, the conceptual model is still of independent requests rather than a session."

A "transfer progress event" does not exist in HTTP. Other (more convoluted) methods have to be used.
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
Both FTP and HTTPS are protocols - a set of rules. HTTP was initially introduced as a "lightweight" equivalent of FTP, intended to be used for short web-page elements rather than files. To quote from Wikipedia ...

"HTTP originally dropped the connection after each transfer because doing so was so cheap. While HTTP has subsequently gained the ability to reuse the TCP connection for multiple transfers, the conceptual model is still of independent requests rather than a session."

A "transfer progress event" does not exist in HTTP. Other (more convoluted) methods have to be used.
Thanks Brian for the historical background. IMO my original point still stands: if convoluted code can be used to simulate an event (in above coding by using the Sleep command), then surely an event could be constructed (with input parameter of time interval) and return parameters of (say) SizeDownloaded and TotalSize with this inbuilt into (say) OkhttpUtils2.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
...if convoluted code can be used to simulate an event ...
Yes - perhaps the code could be implanted in a library and then exposed as an event. But that means that everyone using the library has to carry the burden of the extra code overhead even when making very small data requests so it is not all good news. Without understanding more about the mechanics of HTTP it is difficult to know how to make that choice.
 
Upvote 0
Top