B4J Question API Post /Get with result problem

Peter Lewis

Active Member
Licensed User
HI All

I have been trying to get a currency update from an API. If I put the string in the browser then I get the correct response. Now I have tried thru code mixing and matching info that I got from B4A and at the moment the requests go thru but no return info

If you have any input, please let me know. THX

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    req
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub req

    Dim j As HttpJob

    j.Initialize("",Me)

    j.Download("http://apilayer.net/api/live?access_key=4c3d6103b3a149ce34d0b56b651a5210&currencies=ZAR&format=1")
   

      Dim result As ResultSet 'String
    result= j.Poststring(j.Download)
    'Log(j.GetString(j.Download))   
    Log(result)

    j.GetRequest.SetHeader("Content-Type", "application/json; charset=utf-8")
   

End Sub

Sub JobDone (job1 As HttpJob)
    Dim resultJSON As String
    Log("USDZAR " &  job1.JobName)
    Log("jobsucess " & job1.Success )
    resultJSON = job1.GetString
    Log ("JSON " & resultJSON)

    job1.Release
End Sub
 

EnriqueGonzalez

Well-Known Member
Licensed User
This is the part giving you problems.

B4X:
      Dim result As ResultSet 'String
    result= j.Poststring(j.Download)
    'Log(j.GetString(j.Download))  
    Log(result)

just get rid of it, poststring doesnt return a resulset. You have to use the json library to parse the information coming from the url.

this is the response from the url:

Waiting for debugger to connect...
Program started.
USDZAR
jobsucess true
JSON {
"success":true,
"terms":"https:\/\/currencylayer.com\/terms",
"privacy":"https:\/\/currencylayer.com\/privacy",
"timestamp":1495244052,
"source":"USD",
"quotes":{
"USDZAR":13.226304
}
}
 
Upvote 0
Top