B4J Question wait for and api response

a.consorti

Member
Licensed User
Hi guys! How are you,
I state that I'm new to B4J and, despite my many attempts and various readings, I can't do something that should be simple.
In practice I have created a web portal in B4J and I manage the various pages through the handlers. So far, so good.
The problem arises when I intercept a GET request which must generate a response page that contains other data that I must retrieve via an API call.
From the various tests carried out it would seem that the "Wait For" call does not wait for anything and therefore an empty page is shown to me.
Do you have any suggestions?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 1

peacemaker

Expert
Licensed User
Longtime User
Where needed in the ends of API subs call
B4X:
            If SubExists(callback, "INFERENCE_CONFIG_FINISHED") Then
                CallSubDelayed2(callback, "INFERENCE_CONFIG_FINISHED", result)
            End If

And in the callback class (handlers) - wait for it:
B4X:
Wait for INFERENCE_CONFIG_FINISHED (result As String)
If result = ...

Do not forget to fill the callback object before using API class.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
You're cyberguru in Discord chat yesterday right?
Have you tested my code?

Main:
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private srvr As Server
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.Port = 51042
    srvr.AddWebSocket("/ws", "Index")
    srvr.Start
    StartMessageLoop
    'open browser and navigate to: http://127.0.0.1:51042/
End Sub

B4X:
'WebSocket class
Sub Class_Globals
    Private ws As WebSocket
    Private Section1 As JQueryElement
End Sub

Public Sub Initialize
    
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    Log("Connected")
    ws = WebSocket1
    GenerateSection
End Sub

Private Sub WebSocket_Disconnected
    Log("Disconnected")
End Sub

Private Sub GenerateSection
    Dim sf As Object = GetApiResponse("https://api.xxxxxxxxxxx.xxx/random.php")
    Wait For (sf) Complete (Result As String)
    Section1.SetHtml("Result: " & Result)
    ws.Flush
End Sub

Private Sub GetApiResponse (url As String) As ResumableSub
    Try
        Dim result As String
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(url)
        Wait For (j) JobDone (j As HttpJob)
        If j.Success Then
            result = j.GetString
        Else
            Log(j.ErrorMessage)
        End If
        j.Release
    Catch
        Log(LastException)
    End Try
    Return result
End Sub
 

Attachments

  • ServerCallApi.zip
    9 KB · Views: 61
Upvote 0

a.consorti

Member
Licensed User
Just to update you, I've tried without websockets and the simple API call doesn't wait via the "Wait For". Below I show the code used:

example call api with wait for in server handler:
'Handler class
Sub Class_Globals
    
End Sub

Public Sub Initialize
    
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
        
    Dim sf As Object = GetApiResponse("https://www.boredapi.com/api/activity")
    Wait For (sf) Complete (ApiResult As String)
    
    resp.write (ApiResult)
End Sub



Private Sub GetApiResponse (url As String) As ResumableSub
    Try
        Dim result As String
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(url)
        Wait For (j) JobDone (j As HttpJob)
        If j.Success Then
            result = j.GetString
        Else
            Log(j.ErrorMessage)
        End If
        j.Release
    Catch
        Log(LastException)
    End Try
    Return result
End Sub

The above code, even if correct, only shows a blank page because it doesn't wait for the API call to return the value to be printed on the screen
 
Upvote 0
Top