B4J Question [SOLVED] REST API

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello


I need to translate this into B4j. A customer wants us to send information to their server using REST API. This is new to me. The below image(from customer) shows a VBScript.

1668515750041.png


Any assistance will be appreciated.

Regards

John.
 
Solution
B4X:
    job.Initialize("",Me)
   
    job.PostString(sApiurl,body)
    job.GetRequest.SetHeader("x-api-key",sApiKey)
    job.GetRequest.SetContentType("application/json")
    
' The problem is with the  timeouts 
    job.GetRequest.Timeout  = 1000

    Wait For (job) JobDone(job As HttpJob)

You can only set one timeout from the code.
However, you can use this to set the other timeouts

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
B4X:
    job.Initialize("",Me)
   
    job.PostString(sApiurl,body)
    job.GetRequest.SetHeader("x-api-key",sApiKey)
    job.GetRequest.SetContentType("application/json")
    
' The problem is with the  timeouts 
    job.GetRequest.Timeout  = 1000

    Wait For (job) JobDone(job As HttpJob)

You can only set one timeout from the code.
However, you can use this to set the other timeouts
 
Upvote 0
Solution

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
Public Sub TestRestAPI() As ResumableSub
    Dim Result As String
    Dim sURL As String = "---- URL API REST ----"
    Dim sBodyParameters As String = "---- BODY -----"
    Dim sApiKey As String = "---- API KEY ----"

    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString(sURL, sBodyParameters)
        j.GetRequest.SetHeader("x-api-key", sApiKey)
        j.GetRequest.SetHeader("Content-Type", "application/json")
        SetRequestTimeOut(3000, 3000, 65000)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        Else
            Result = j.ErrorMessage
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub

Public Sub SetRequestTimeOut(ConnectTime As Long, CallTime As Long, ReadTime As Long)
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
    Dim TimeUnit As JavaObject
    TimeUnit.InitializeStatic("java.util.concurrent.TimeUnit")
    builder.RunMethod("connectTimeout", Array(ConnectTime, TimeUnit.GetField("MILLISECONDS")))
    builder.RunMethod("callTimeout", Array(CallTime, TimeUnit.GetField("MILLISECONDS")))
    builder.RunMethod("readTimeout", Array(ReadTime, TimeUnit.GetField("MILLISECONDS")))
    jo.SetField("client", builder.RunMethod("build", Null))
End Sub

Option: See resolve

Ctrl-B
1668518414889.png
 
Upvote 0
Top