Hi all,
I started a little console application I wanna do with B4J.
App is simple, I have to connect to a postgres database (done) retrieve data from a table, iterate over it, and for each record I have to do a web request to retrieve a JSON to parse and take several URLs, then I have to download this images/files with that URL and fill a CSV. It's an easy task, and I started playing with pgsql connection (done) a simple select to try (done) and now I'm working with the communication with the API. I connect and retrieve data (the access_token for start) following jOkHttpUtils2 post samples. And it works, but I noticed getToken request is async, and I need to be synchronous because I can't continue with operations until I get this validation.
This is my code:
As far as I undersand this code is async because the Wait, but "Wait" resumes the execution of my app, and I need to really wait until I get the token to continue processing other things.
I tried to remove Wait command and move the code after it, to a Sub called "JobDone(j as HttpJob)" and this worked, but this way all jobs done will end there and I have to control there the name of the job and so on to do one or other things, and I don't see it too much elegant. I tried calling it GetToken_JobDone as I remember to see this kind of nomenclature some years ago, and IDE don't warning about sub not being called, but debuging, is never called.
What I'm doing wrong?
I started a little console application I wanna do with B4J.
App is simple, I have to connect to a postgres database (done) retrieve data from a table, iterate over it, and for each record I have to do a web request to retrieve a JSON to parse and take several URLs, then I have to download this images/files with that URL and fill a CSV. It's an easy task, and I started playing with pgsql connection (done) a simple select to try (done) and now I'm working with the communication with the API. I connect and retrieve data (the access_token for start) following jOkHttpUtils2 post samples. And it works, but I noticed getToken request is async, and I need to be synchronous because I can't continue with operations until I get this validation.
This is my code:
B4X:
..
..
'ConnectToDatabase
GetToken
StartMessageLoop 'only required in a console app
End Sub
Sub GetToken() 'As String
Dim j As HttpJob
j.Initialize("", Me)
j.PostString(baseURLToken, baseBodyToken)
Wait For (j) JobDone(j As HttpJob) ' -- para peticiones asyncronas
If j.Success Then
Dim jp As JSONParser
jp.Initialize(j.GetString)
Dim response As Map = jp.NextObject
token = response.Get("access_token")
'Return response.Get("access_token")
Else
Log(j.Response.ErrorResponse)
End If
j.Release
End Sub
As far as I undersand this code is async because the Wait, but "Wait" resumes the execution of my app, and I need to really wait until I get the token to continue processing other things.
I tried to remove Wait command and move the code after it, to a Sub called "JobDone(j as HttpJob)" and this worked, but this way all jobs done will end there and I have to control there the name of the job and so on to do one or other things, and I don't see it too much elegant. I tried calling it GetToken_JobDone as I remember to see this kind of nomenclature some years ago, and IDE don't warning about sub not being called, but debuging, is never called.
What I'm doing wrong?