B4J Tutorial [BANano] Ajax calls

EDIT 1.04: Method syntaxes have changed!

With BANano, it is easy to make a Ajax call (e.g. to call a B4J REST API method).

There are two ways to do so:

1. Using events (the prefered way, as it keeps your app responsive if it is a long call)

B4X:
    ...
    BANano.CheckInternetConnection("ForAjaxCallFromButton") 
End Sub

Sub BANAno_IsConnectedResult(Tag As String, Result As Boolean)
   Log("Internet for: " & Tag & " = " & Result)
   Select Case Tag
       Case "ForAjaxCallFromButton"
           If Result Then               
               Dim headers As Map
               headers.Initialize
               headers.put("Content-Type", "application/json")
               BANano.CallAjax("https://reqres.in/api/users?page=1","GET","jsonp", "","ID0001", False, headers)
           Else
               MiniCSS.Content("#r4c1", "contajax", "Cannot make Ajax call because you are not connected to the internet, but here is the last result:<br>" & Shared.Ajaxresult)
           End If
   End Select
End Sub

Sub BANano_CallAjaxResult(Success As Boolean, UniqueID As String, Result As String) 
   Shared.Ajaxresult = Result
   MiniCSS.Content("#r4c1", "contajax", Result)
End Sub

2. Wait (from 1.02+)

Does the same as the above:
B4X:
If BANano.CheckInternetConnectionWait Then       
       Dim headers As Map
       headers.Initialize
       headers.put("Content-Type", "application/json")
       Dim res As String = BANano.CallAjaxWait("https://reqres.in/api/users?page=1","GET","jsonp", "", False, headers)
       Shared.Ajaxresult = res
       MiniCSS.Content("#r4c1", "contajax", res)
   Else
       MiniCSS.Content("#r4c1", "contajax", "Cannot make Ajax call because you are not connected to the internet, but here is the last result:<br>" & Shared.Ajaxresult)
   End If

Alain
 
Last edited:
Top