Android Question Download more than one file at the same time (OkHttp)

nwhitfield

Active Member
Licensed User
Longtime User
You can't. An HTTP request can only ever request one thing at a time. What you have to do is create multiple requests.

Create a simple (resumable) sub that downloads a file passed to it, and does whatever you need, and then call it three times.

B4X:
downloadFile("http://example.com/file1.zip")
downloadFile("http://example.com/file2.zip")

Sub downloadFile ( url as string ) 
dim job as httpjob
job.initialize("",Me)
job.download(url)

wait for (job) JobDone ( job as HttpJob)
if job.success then
' do your sutff; maybe save it to File.DirInternal ?
end if
job.release
end sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
Lots of ways - and to an extent it depends what you're doing with the files afterwards. You could, for example, have a global integer, set it to zero, and have the sub increment it by one each time you finish processing the file.*

Or if you know what the files will be called (say, you're going to save them with particular names to the internal memory) then you might just check to see if all those files have appeared yet.

* I'm wondering about a race condition here? I don't know if that's possible in B4A; it might be better to have a list containing a map for each file name, with the name, and the download status. So after the wait for , you'd add an item to the list with something like

B4X:
DownloadedFiles.Add(CreateMap("file" : url, "downloaded" : job.success))

Then elsewhere in your code, you can check to see the size of the DownloadedFiles list, and iterate over it to see if all the files did download correctly
 
Upvote 0
Top