Android Code Snippet [B4X] One-liner downloads

Discussion in 'Code Snippets' started by Erel, Jul 18, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Add this sub to a code module or the current module:
    Code:
    Sub Download (Callback As Object, link As StringAs HttpJob
       
    Dim j As HttpJob
       j.Initialize(
    "", Callback)
       j.Download(link)
       
    Return j
    End Sub
    Now you can make downloads with this code:
    Code:
    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:
    Code:
    Dim j1 As HttpJob
    j1.Initialize(
    "", Me)
    j1.Download(link)
    Wait For (j1) JobDone(j2 As HttpJob'j2 = j1
     
    Johan Hormaza, asales, Segga and 22 others like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice