Android Code Snippet [B4X] One-liner downloads

Add this sub to a code module or the current module:
B4X:
Sub Download (Callback As Object, link As String) As HttpJob
   Dim j As HttpJob
   j.Initialize("", Callback)
   j.Download(link)
   Return j
End Sub

Now you can make downloads with this code:
B4X:
Wait For (Download(Me, "http://www.example.com")) JobDone (j As HttpJob)
If j.Success Then
   Log(j.GetString)
End If
j.Release

You can safely call it multiple times. The events will reach the correct resumable sub instance.

The trick here is that we are using the sender filter parameter to call the sub (which returns the Job which is the sender filter).

It is equivalent to calling:
B4X:
Dim j1 As HttpJob
j1.Initialize("", Me)
j1.Download(link)
Wait For (j1) JobDone(j2 As HttpJob) 'j2 = j1
 
Top