Downloading two archives using HttpUtils

copiloto

Member
Licensed User
Longtime User
Hello,

I'm a very newbie about httputils, and have been reading two or more posibilities about how to download archives from a http web.

Now, at the moment, my prefered way (the simplest for me) is the next code. But, why doesn't download the two archives but only one??


Thanks for reading!


B4X:
'Activity module
Sub Process_Globals
   Dim image As Bitmap
End Sub

Sub Globals
   Dim btnDownload As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   'check if we already loaded the image previously.
   If image.IsInitialized Then
      Activity.SetBackgroundImage(image)
   End If
End Sub

Sub Activity_Resume
   'check if download has finished while the activity was paused
   If btnDownload.Enabled = False AND DownloadService.JobStatus = DownloadService.STATUS_DONE Then
      FinishDownload
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnDownload_Click
   Activity.Color = Colors.Black
   DownloadService.URL = "http://www.b4x.com/basic4android/images/designer1.png"
   DownloadService.Target = File.OpenOutput(File.DirRootExternal, "image.jpg", False)
   StartService(DownloadService)
   
   DownloadService.URL = "http://www........"
   DownloadService.Target = File.OpenOutput(File.DirRootExternal, "text.txt", False)
   StartService(DownloadService)
   
   btnDownload.Enabled = False
End Sub

Sub FinishDownload
   'Load the saved image
   If DownloadService.DoneSuccessfully = True Then
      'image = LoadBitmapSample(File.DirInternalCache, "image.png", _
      ' 100%x, 100%y)
      'Activity.SetBackgroundImage(image)
   End If
   btnDownload.Enabled = True
   DownloadService.JobStatus = DownloadService.STATUS_NONE
End Sub
 

copiloto

Member
Licensed User
Longtime User
Hi Erel,

I've been studying it and... yes, it's much more easy. Here my (your) modified code for anyone who could need it.

Now, it's time to learn DBUtils :)

Thanks Erel.
Best regards!


Sub Process_Globals
Dim Job2Links As List
Dim text As String
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("mylayout.bal")
End Sub

Sub Activity_Resume
'Check whether a job has finished while the activity was paused.
If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

Sub UrlDone(Url As String)
Log(Url & " done")
End Sub

Sub JobDone (Job As String)
For i = 0 To HttpUtils.Tasks.Size - 1
link = HttpUtils.Tasks.Get(i)
If HttpUtils.IsSuccess(link) Then
text=text & HttpUtils.GetString(link)
End If
Next
File.WriteString(File.DirRootExternal,"fullarchive.txt",texto) HttpUtils.Complete = False 'Turn off the complete flag so we won't handle it again if the activity is resumed.
Msgbox("Download finished!!","")
End Sub

Sub Button1_Click
text=""
HttpUtils.CallbackActivity = "Main"
HttpUtils.CallbackJobDoneSub = "JobDone"
HttpUtils.CallbackUrlDoneSub = "UrlDone"
Job2Links.Initialize
Job2Links.AddAll(Array As String("http://your www.","https://your https://www. (yes! even https works!!)")
HttpUtils.DownloadList("Job2", Job2Links)
End Sub
 
Upvote 0
Top