B4J Question use an API

Alexander Stolte

Expert
Licensed User
Longtime User
please help me how do I put this into a code and assign data to a variable
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim size As Int = root.Get("size")
Dim data As List = root.Get("data")
For Each coldata As String In data
Next
Dim success As String = root.Get("success")
Dim length As Int = root.Get("length")
Dim type As String = root.Get("type")
 
Upvote 0

Anurag Jain

Member
Licensed User
hi, I am getting some error, shows http must be assigned a value before it has to be used
I understand its some minor issue, please help. I am new to this, but we need to conduct an experiment

api use:
Private Sub api
        
    
    Dim parser As JSONParser
    parser.Initialize(<https://qrng.anu.edu.au/api/jsonI.php?length=10&Type=hex16&size=2>)
    Dim root As Map = parser.NextObject
    Dim size As Int = root.Get("size")
    Dim data As List = root.Get("data")
    For Each coldata As String In data
    Next
    Dim success As String = root.Get("success")
    Dim length As Int = root.Get("length")
    Dim Type As String = root.Get("type")

    End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
see

B4X:
Public Sub TestAPIGetQrngURL
    Dim BaseURL As String = "https://qrng.anu.edu.au/API/jsonI.php"

    Dim Parameters() As String = Array As String ("length", 10, "type", "hex16", "size", 2)
    Wait For (APIGetQrngURL(BaseURL, Parameters)) Complete (APIResult As String)
    Log(APIResult)
  
    Dim parser As JSONParser
    parser.Initialize(APIResult)
    Dim mRoot As Map = parser.NextObject
    Dim msuccess As String = mRoot.Get("success")

    If msuccess = True Then
        Dim mlength As Int = mRoot.Get("length")
        Dim mType As String = mRoot.Get("type")
        Dim msize As Int =  mRoot.Get("size")

        Log(msuccess)
        Log(mlength)
        Log(mType)
        Log(msize)
  
        Dim data As List = mRoot.Get("data")
        For Each coldata As String In data
            Log(coldata)
        Next
    End If

End Sub

Public Sub APIGetQrngURL(URL As String, Parameters() As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
    Return ResultURL
End Sub

1625678925622.png
 
Upvote 0

Anurag Jain

Member
Licensed User
Thanks a lot, this is working great. I am working on an experiment, will share back with the community once this is complete
 
Upvote 0

Anurag Jain

Member
Licensed User
see sample:

Thanks a lot, this working great, many many thanks again
 
Upvote 0
Top