Android Question API call from server

santiago

Member
Licensed User
Longtime User
Hello,
I trying to call an API with parameters.

here is my code


Code: Copy
Dim res() As Double = Array As Double(9999, 9999)
    Dim j As HttpJob
    j.Initialize("", Me)
    
    'values for the call'
    Dim refee As String="WPCD"
    Dim FECHAI As String="2021-09-01"
    Dim horai As String="11:00"
    Dim a As String="3"
    Dim n As String="2"
    Dim pa As String="1200"
    Dim pn As String="0600"
    
    j.Download2("http://xxxx.xxxx.xxxx.xxxx:53/API/FH/W0082", Array As String("refee", refee, "fechai", FECHAI,"horai",horai,"a",a,"n",n,"pa",pa,"pn",pn))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim m As Map = jp.NextObject
        
    Else
        Log("Error!")
    End If
    j.Release

I would like to call
?refee=xxx&fechai=xxx&horai=xxx&a=x&n=x&pa=x&pn=x (x are values)
and receive a response with a value from server

Please , what I am doing wrong?
The API in server is public, not authentification is required, only feed the data

Thanks in advance
 
What is the error? Change
Code: Copy
Log("Error!")
to
Code: Copy
Log($"Error: ${j.ErrorMessage}"$)
and see if the server responds with an error message
 
Upvote 0
test:

B4X: Copy
Public Sub TestAPIGetURL
    Dim BaseURL As String = "http://xxxx.xxxx.xxxx.xxxx:53/API/FH/W0082"

    Dim refee As String="WPCD"
    Dim FECHAI As String="2021-09-01"
    Dim horai As String="11:00"
    Dim a As String="3"
    Dim n As String="2"
    Dim pa As String="1200"
    Dim pn As String="0600"
    
    Dim Parameters() As String = Array As String("refee", refee, "fechai", FECHAI, "horai", horai, "a", a, "n", n, "pa", pa, "pn", pn)
    Wait For (APIGetURL(BaseURL, Parameters)) Complete (DataResult As String)
    If DataResult = "" Then Return
    
    Dim Data As Map = DataResult.As(JSON).ToMap 'ignore
    Log(Data)
        
End Sub

Public Sub APIGetURL(URL As String, Parameters() As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json")
        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
 
Upvote 0
test:

B4X: Copy
Public Sub TestAPIGetURL
    Dim BaseURL As String = "http://xxxx.xxxx.xxxx.xxxx:53/API/FH/W0082"

    Dim refee As String="WPCD"
    Dim FECHAI As String="2021-09-01"
    Dim horai As String="11:00"
    Dim a As String="3"
    Dim n As String="2"
    Dim pa As String="1200"
    Dim pn As String="0600"
   
    Dim Parameters() As String = Array As String("refee", refee, "fechai", FECHAI, "horai", horai, "a", a, "n", n, "pa", pa, "pn", pn)
    Wait For (APIGetURL(BaseURL, Parameters)) Complete (DataResult As String)
    If DataResult = "" Then Return
   
    Dim Data As Map = DataResult.As(JSON).ToMap 'ignore
    Log(Data)
       
End Sub

Public Sub APIGetURL(URL As String, Parameters() As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json")
        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
oparra, thanks for your time.
The code is working properly.I made a code mistake in the server before first log's saving
But I will check your code in order to test speed
 
Upvote 0
Back
Top