B4J Question [ABMaterial] Is possible to use API to show info?

hibrid0

Active Member
Licensed User
Longtime User
HI I like to know if is possible to use abmaterial to connect to Api and not direct to BD.
 

hibrid0

Active Member
Licensed User
Longtime User
No idea what you are talking about. If you mean calling anther API then you can do this on the server side using e.g. the jOkHttpUtils2 library. Please explain a bit further what you are trying to do.
HI sorry if not explain, Where I work have an API restful, I want to connect to this api and get data yo show on a Webapp.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Using the jOkHttpUtils2 library will be your solution then. Note that an ABM Webapp can use ANY B4J library as it runs on the server side. For example, we have a WebApp where the data is shown received with the jMQTT library.

Here is how I call a Rest API from within my WebApp (in this example it is my own Rest API which calls another Rest API but the principle is the same):
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    resp.ContentType = "application/json"
    Dim Response As String

    Select Case req.Method
        Case "GET"
            If req.RequestURI.Length + 1 <= "/v1/admin/monitor/".Length Then
                ABMShared.SendError(resp, 404, "Invalid call")
                Return
            End If
            Dim PostType As String = req.RequestURI.SubString("/v1/admin/monitor/".Length)
            Select Case PostType
                Case "latest"
                    RunAPI(resp) '<---- calling the Rest API
                    StartMessageLoop
                Case Else
                    ABMShared.SendError(resp, 404, "Invalid call")
                    Return
            End Select
        Case "POST" ' Update a scanner assignment
...

Sub RunAPI(resp As ServletResponse)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Main.MonitorUrl & "/monitor/latest")
    j.GetRequest.SetHeader("api_key", "eyJhbGciOiJIUzUxMiIsInppc......")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'Log("Success")
        Dim body As TextReader
        body.Initialize(j.GetInputStream)
        resp.Write(body.ReadAll)
    Else
        resp.Status = j.Response.StatusCode
        resp.Write(j.Response.ErrorResponse)       
    End If
    j.Release
    StopMessageLoop
End Sub

Alwaysbusy
 
Last edited:
Upvote 0
Top