[Class] Progress bar for HttpUtils2

admac231

Active Member
Licensed User
Longtime User
Not so much a class per se but it seems a few people were after this based on this thread. Feel free to move this to somewhere more appropriate if need be.

Here's an example of usage:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim pro As ProgressBar
   pro.Initialize("pro")
   Dim cmdSubmit As Button
   cmdSubmit.Initialize("cmdSubmit")
   cmdSubmit.Text = "Submit"
   HttpUtils2Service.progressSub = "downloadProgress"
   HttpUtils2Service.timerInterval = 200
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.AddView(pro,5dip,5dip,100%x-10dip,20dip)
   Activity.AddView(cmdSubmit, 5dip, 30dip, 100%x-10dip,50dip)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub downloadProgress(tmp As ProgressStatus)
   Log(tmp.Downloaded & "/" & tmp.Total)
   pro.progress=Round((tmp.downloaded*100)/tmp.total)
End Sub

Sub cmdSubmit_Click
   Dim jobPost As HttpJob
   jobPost.Initialize("JobPostName",Me)
   jobPost.Download("http://ipv4.download.thinkbroadband.com/5MB.zip")
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "JobPostName"
                ToastMessageShow("Success!", True)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

To activate the progress bar you have to set HttpUtils2Service.progressSub with your update callback sub as well as setting HttpUtils2Service.timerInterval (int in ms). If HttpUtils2Service.progressSub = "" then HttpUtils2 runs as normal.

Bear in mind you have to use the modified HttpUtils2Service.bas and HttpJob.bas included in the zip. You can't just copy and paste the above code.
 

Attachments

  • HttpUtils2wProgress.zip
    8.4 KB · Views: 789

johnaaronrose

Active Member
Licensed User
Longtime User
admac31,
Thanks for directing me to this thread from the 'Download Progress Feature for HTTPUtils when downloading file' thread. I've just used your code and it works on my app. I altered the Httputils2Service module to make the variable filename public so that I could reference it in my module. this allowed me to copy it (in my Main module) to my sdcard. Thanks for your help.
 

pluton

Active Member
Licensed User
Longtime User
Thank You, thank you.

Finally, working with HttpUtils2.
Last week I have some project in which I needed progress bar so I must work with HttpUtils because there wasn't class for progress bar in HttpUtils2

Thank you again
 

adamioan

Member
Licensed User
Longtime User
Thanks admac231 for the module.
Please mind the
B4X:
CallSub2(target,"downloadProgress",tmp)
at the end of the HttpUtils2Service.
It must be changed with
B4X:
CallSub2(target,progressSub,tmp)
in order to get it to work under code obfuscation, and of course remember to set the progressSub name to a string with an underscore, such as Download_Complete.
Thanks again
 

Mahares

Expert
Licensed User
Longtime User
@admac: I downloaded the 5MB.zip file to the root of my sd card, but the name shown on the SD card is called: "1" . How can I change it in your code to retain the same name.
Thank you
 

Mahares

Expert
Licensed User
Longtime User
I came up with a solution, althought not sure if it is the most plausible. Someone else may have a better method. I love to see it. Here is mine:
I added this code to Main:
B4X:
MyFile="http://ipv4.download.thinkbroadband.com/5MB.zip"   
MyFile=MyFile.Replace(QUOTE,"")
MyFile=MyFile.Replace("http://ipv4.download.thinkbroadband.com/","")

Then: I replaced this line:
B4X:
Response.GetAsynchronously("response", File.OpenOutput(TempFolder,task, False), _
      True, TaskId)
With this line:
B4X:
Response.GetAsynchronously("response", File.OpenOutput(TempFolder, Main.MyFile, False), _
      True, TaskId)
 

basil99

Active Member
Licensed User
Longtime User
Hi, admac231!

I need to DL several files at once and need progress bar for each file and probably overall progress, can you point me how to implement progress bar for this purposes ?

thank you
 
Top