Android Question B4XPages project hangs on download of 2MB file

johnaaronrose

Active Member
Licensed User
Longtime User
I'm using 'Wait For' in a B4XPages project calling the OkhttpUtils2 library to download a 2MB file. I've attached a zip file of the project. The relevant code in B4XHTTPSPage (as B4XMainPage is just code for a button to show B4XHTTPSPage) is:
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private DBFolder As String
    Private HTTP1 As HttpJob
    Private DBFilename As String = "JewishWorld.db"
    Private DBSize As Long
    Private DBTotalDownloaded As Long
    Private DBTotalOutOf As Long
    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("HTTPS")
    B4XPages.SetTitle(Me, "HTTPS")
           
    DBFolder = xui.DefaultFolder
   
    Dim ServerPath As String = "rose.myddns.me/" & DBFilename
    HTTP1.Initialize("", Me)
    Log("HTTPS.Initialized")
    HTTP1.Download(ServerPath)
    Log("Started https")
    DownloadAndTrackProgress
End Sub
   
Private Sub DownloadAndTrackProgress
    Dim j As HttpJob
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        Log("File was downloaded successfully")
    Else
        Log(j.GetString)
        Log("Error downloading file")
    End If
    j.Release
       
    DBSize = File.Size(DBFolder, DBFilename)
    Log("Database  : " & Round(DBSize / 1000) & "KB")
    Log("Downloaded: " & Round(DBTotalDownloaded / 1000) & "KB")
    Log("Out of    : " & Round(DBTotalOutOf / 1000) & "KB")
    pbDownload.Progress = Round((DBTotalDownloaded*100)/DBTotalOutOf)  
End Sub

However, the app hangs after installing it using B4A-Bridge. The log is attached as a screenshot as I was not able to work out how to copy it as a text file.
I can download the file by using the url in Firefox. However, Firefox pops up a window asking me if I want to save the file. Does the OkhttpUtils2 library automatically respond to this window?
PS I want to show the progress of the download in a ProgressBar. But I'll ask that on another thread if Erel says that I should.
 

Attachments

  • B4A HTTPS Screenshot.png
    B4A HTTPS Screenshot.png
    6 KB · Views: 115
  • HTTPSExample.zip
    11.5 KB · Views: 124
Last edited:

Spavlyuk

Active Member
Licensed User
DownloadAndTrackProgress isn't doing anything since it creates a new empty request. You should probably use the HTTP1 variable.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
HTTP1.Initialize("", Me) Log("HTTPS.Initialized") HTTP1.Download(ServerPath) Log("Started https") DownloadAndTrackProgress End Sub Private Sub DownloadAndTrackProgress Dim j As HttpJob Wait For (j) JobDone(j As HttpJob) If j.Success Then Log(j.GetString) Log("File was downloaded successfully") Else Log(j.GetString) Log("Error downloading file") End If j.Release
You are not using okhttputils correctly. You are using two different httpjobs. BOTH WRONG.
Start again using the tutorial.

Your serverpath is not a valid URL btw. It must start with http:// or https://

You also forgot to save the downloaded file to disc!

Your Code does not show any progress when downloading either! The calculated progress in your code you do the calculation on the existing file.
 
Last edited:
Upvote 1

johnaaronrose

Active Member
Licensed User
Longtime User
@DonManfred I've amended project and it now downloads Ok and is copied to xui.DefaultFolder But I still don't know how to monitor progress in the code shown below. What is required to change in the code?
Not sure which tutorial you're referring to: it's very confusing with the moves to okhttputils2 & B4XPages and the recommended use of 'Wait For'. Do you mean https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/ ?

Coding is now:
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private DBFolder As String
    Private DBFilename As String = "JewishWorld.db"
    Private DBSize As Long
    Private DBTotalDownloaded As Long
    Private DBTotalOutOf As Long
    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("HTTPS")
    B4XPages.SetTitle(Me, "HTTPS")
          
    DBFolder = xui.DefaultFolder
End Sub

Private Sub B4XPage_Appear
    DownloadAndTrackProgress
End Sub
  
Private Sub DownloadAndTrackProgress
    Dim HTTP1 As HttpJob
    Dim ServerPath As String = "https://rose.myddns.me/" & DBFilename
    HTTP1.Initialize("", Me)
    Log("HTTPS.Initialized")
    HTTP1.Download(ServerPath)
    Log("Started https")
    Wait For (HTTP1) JobDone(HTTP1 As HttpJob)
    If HTTP1.Success Then
        Log(HTTP1.GetString)
        Log("File was downloaded successfully")
        Dim out As OutputStream = File.OpenOutput(DBFolder, "JewishWorld.db", False)
        File.Copy2(HTTP1.GetInputStream, out)
        out.Close '<------ very important
        Log("File was copied successfully")
    Else
        Log(HTTP1.GetString)
        Log("Error downloading file")
    End If
    HTTP1.Release
      
    DBSize = File.Size(DBFolder, DBFilename)
    Log("Database  : " & Round(DBSize / 1000) & "KB")
    Log("Downloaded: " & Round(DBTotalDownloaded / 1000) & "KB")
    Log("Out of    : " & Round(DBTotalOutOf / 1000) & "KB")
    pbDownload.Progress = Round((DBTotalDownloaded*100)/DBTotalOutOf) 
End Sub

Sub HTTP1_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    Dim s As String
    s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
    pbDownload.Progress = Round((TotalDownloaded*100)/Total)
    DBTotalDownloaded = TotalDownloaded
    DBTotalOutOf = Total
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

PS I saw somewhere about having HU2_PUBLIC in Build Configuration. What does it do?
PPS It's a nuisance to take a screenshot of the log. Is there any way to download the log's contents to a text file?
 

Attachments

  • B4AHTTPSScreenshot from 2022-02-18 12-16-31.png
    B4AHTTPSScreenshot from 2022-02-18 12-16-31.png
    9.7 KB · Views: 124
Last edited:
Upvote 0
Top