Android Question Technique on how to implement blocking HTTP calls?

wimpie3

Well-Known Member
Licensed User
Does anyone know how to implement blocking HTTP calls in B4A? Currently, I'm sending out a HTTP command and I process the rest of my code in the response Sub. However, I need to send out tens of commands one after the other, which makes my code very messy. Is it possible to do something like this:

Sub()
sendHTTP
waitforreturn..
sendHTTP
waitforreturn...
end Sub

I think this is called "blocking" http calls in VB6...
 

DonManfred

Expert
Licensed User
See this example.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    lv.FastScrollEnabled = True
    lv.SingleLineLayout.ItemHeight = 30dip
  lv.SingleLineLayout.Label.TextSize = 12
  lv.SingleLineLayout.Label.TextColor = Colors.Green
  lv.SingleLineLayout.Label.Gravity = Gravity.CENTER
    lv.TwoLinesLayout.ItemHeight = 60dip
    lv.TwoLinesLayout.Label.TextSize = 10
    lv.TwoLinesLayout.Label.TextColor = Colors.Red
    lv.TwoLinesLayout.Label.Gravity = Gravity.CENTER
    lv.TwoLinesLayout.SecondLabel.TextSize =12
    lv.TwoLinesLayout.SecondLabel.TextColor = Colors.Red
    lv.TwoLinesLayout.SecondLabel.Typeface = Typeface.DEFAULT_BOLD
    joblist.Initialize
    jobresults.Initialize
    If FirstTime Then
  End If
    ' You can fill this list with the jobs you have to do...
    joblist.Add("dummy")
    joblist.Add("dfghdfgd")
    joblist.Add("dfgertzer")
    joblist.Add("rtertertet")
    joblist.Add("tewtwete")
    joblist.Add("init")
    joblist.Add("tabellen")
    joblist.Add("paarungen")
  
  
    If joblist.Size > 0 Then
        Label1.Text = "Executing Job "&joblist.Get(0)&"..."
        ExecuteRemoteQuery(joblist.Get(0),joblist.Get(0))
        ' the further logic goe on in sub FTP_DownloadCompleted
    End If
End Sub

Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    Dim dummy As String
    job.Initialize(JobName, Me)
    job.download2("http://vdfb.de/b4a/", Array As String( _
        "action", Query _
    ))
End Sub
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    ' Delete the topmost job in ftpjobs
    If joblist.Size > 0 Then
        joblist.RemoveAt(0)
    End If

    If Job.Success Then
        Dim res As String
        res = Job.GetString
        If Job.JobName = "tabellen" Then
            Log("JobName: "&Job.JobName&"/Response:" & res)
        Else If Job.JobName = "paarungen" Then
            Log("JobName: "&Job.JobName&"/Response:" & res)
        Else If Job.JobName = "Init" Then
            Log("JobName: "&Job.JobName&"/Response:" & res)
        Else
            ToastMessageShow(Job.JobName&": " & Job.GetString, True)
        End If
        Dim resmap As Map
        resmap.Initialize
        resmap.Put("Job",Job)
        resmap.Put("Success",Job.Success)
        resmap.Put("Result",Job.GetString)
        jobresults.Add(resmap)
        lv.AddSingleLine(Job.JobName)
        lv.SetSelection(lv.Size-1)  

        If joblist.Size > 0 Then
            Label1.Text = "Executing Job "&joblist.Get(0)&"..."
            ExecuteRemoteQuery(joblist.Get(0),joblist.Get(0))
        Else
            Label1.Text = "All Jobs are done"
            Log("All Jobs are done")
            For i = 0 To jobresults.Size -1
                Log(jobresults.Get(i))
            Next
        End If

    Else
        Dim resmap As Map
        resmap.Initialize
        resmap.Put("Job",Job)
        resmap.Put("Success",Job.Success)
        resmap.Put("Exception","Error: " & Job.ErrorMessage)
        jobresults.Add(resmap)
        lv.AddTwoLines(Job.JobName, Job.ErrorMessage)
        lv.SetSelection(lv.Size-1)  
        'ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

wimpie3

Well-Known Member
Licensed User
Excellent idea... I'm using a list now to feed individual HTTP requests, thanks!
 
Top