B4J Question [Solved] Download Files from server with progress

Dadaista

Active Member
Licensed User
Hi all
How can I do something like this
from B4J?
 

Erel

Administrator
Staff member
Licensed User
B4A + B4J solution:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Private Sub Button1_Click
    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
        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

You need to add to the build configuration (Ctrl + B): HU2_PUBLIC
 
Upvote 0

Dadaista

Active Member
Licensed User
Thank you very much Erel
I am going to try to add to my project.
Where I must add " HU2_PUBLIC"?... My IDE is in spanish... 1st, 2nd or 4th field?...
2021-01-22.png

Must I add something else to
B4X:
#CustomBuildAction: After Packager, C:\Program Files (x86)\Inno Setup 6\.....
 
Upvote 0

Dadaista

Active Member
Licensed User
Hi!
The file is saved correctly. It size is 0k
But, why in this line
B4X:
Log(size & ", " & TotalLength)
Size is always = 0? :rolleyes:
B4X:
0, 16777216
0, 16777216
0, 16777216
0, 16777216
0, 16777216
0, 16777216
0, 16777216
0, 16777216
0, 16777216
How can I solve it?
 
Upvote 0

Dadaista

Active Member
Licensed User
Create a small project where it doesn't work and upload it.

It is amazing. Exactly same subs in both projects and in my project does not work... hahahahahaha🙄😲🥴

Well.. in Debug mode does not work but in Release (uploaded project)
 

Attachments

  • ProjectDnT.zip
    2.6 KB · Views: 64
Upvote 0

Dadaista

Active Member
Licensed User
Not in my computer

in debug mode:
B4X:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
Ha ocurrido un error en la línea: 56 (Main)
java.lang.NullPointerException
    at b4j.example.main$ResumableSub_DownloadAndTrackProgress.resume(main.java:269)
    at b4j.example.main._downloadandtrackprogress(main.java:215)
    at b4j.example.main$ResumableSub_Button1_Click.resume(main.java:162)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:136)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:85)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1086)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
I understand. It is related to a bug in debug mode that is already fixed in the unreleased version that I'm using.

Try this:
B4X:
Private Sub DownloadAndTrackProgress (url As String, sFile As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(url)
    #if debug
    'delete if using B4J v9.0+
    Sleep(0)
    #End If
    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
   
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirTemp, sFile, False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
    End If
   
    j.Release
    Return j.Success
End Sub
 
Last edited:
Upvote 0

Dadaista

Active Member
Licensed User
I understand. It is related to a bug in debug mode that is already fixed in the unreleased version that I'm using.

Try this:
B4X:
Private Sub DownloadAndTrackProgress (url As String, sFile As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(url)
    #if debug
    'delete if using B4J v10.8+
    Sleep(0)
    #End If
    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

    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirTemp, sFile, False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
    End If

    j.Release
    Return j.Success
End Sub

@Erel !!!!

Now is Working in my project!!.. Changing existing sub for above sub 🤣

The line
B4X:
Sleep(0)
was the key

EDIT!!
In my project in release mode does not work. I have deleted the lines
B4X:
#if debug.... #End If

Now works fine
 
Last edited:
Upvote 0
Top