Android Question REST api need to be synchronously

rosippc64a

Active Member
Licensed User
Hi All!
I have a REST API interface what made by other programmer. There are a lot of endpoint and get, post, put delete methods. All works well for testing. I can see the result in the log. These methods I need reach from different activity, so I put them together in a service. When I make test query from it, I can see the result well.
BUT
In the real life I have to make query like this:
B4X:
If CallSub("REST","Stores_Get") > 0 Then
        'there are stores          
        Dim jp As JSONParser
        jp.Initialize(File.ReadString(File.DirInternal,"Stores_Get.json"))
        Try
            Dim l As List = jp.NextArray
            For i = 0 To l.Size-1
                '[{"id":"12","code":"{0C2576F1-4430-41DD-B652-757521A7D76C}","name":"Raktár 1","desc":""}
                Dim m As Map = l.Get(i)
                If CallSub2("REST","Liststock_Get_stockload",m.Get("id")) > 0 Then
                    '[{"itemid":"1","quantity":"35","moving":"0","ean":"0001","name":"Termék 1","unit":"db","cikkszam":"0001"}
                    'there are items for that store
                    Spinner1.Add(m.Get("id") & "; " & m.Get("name") & "; *")
                Else
                    Spinner1.Add(m.Get("id") & "; " & m.Get("name") & ";  ")
                End If
            Next
        Catch
            Log("JSON parser error " & LastException.Message)
        End Try
    End If
of course that can't works, because of asynchronous httpjob query and I can't get immediately result:
B4X:
Sub Stores_Get'
   rGlobal_Get2(eName,Null)
End Sub
Sub Global_Get2(eName As String,param As Map)
   Dim s As String = ""
   If param.IsInitialized Then
       For i = 0 To param.Size-1
           s = s & "&" & param.GetKeyAt(i) & "=" & param.GetValueAt(i)
       Next
   End If
   Dim jobg As HttpJob
   jobg.Initialize(eName & "_Get", "REST")
   s = $"http://${serverip}:8080/${eName.ToLowerCase}?device=${ device }${ s }"$
   jobg.Download(s)
   jobg.GetRequest.Initializeget(s)
   Wait For (jobg) JobDone(j As HttpJob)
   Log(JobDoneX(jobg))
End Sub
Sub JobDoneX (J As HttpJob)
   Log("JobName = " & J.JobName & ", Success = " & J.Success)
   If J.Success Then
       Log(J.GetString)
       If J.JobName.EndsWith("_Get") Then
           File.WriteString(File.DirInternal,J.JobName & ".json",J.GetString)
       End If
       If J.GetString.Length>0 Then
           Dim jp As JSONParser
           jp.Initialize(J.GetString)
           Try
               Dim l As List = jp.NextArray
               Dim m As Map = l.Get(l.Size-1)
               If m.ContainsKey("result") Then
                   restresult = m.Get("result")
               Else
                   restresult = "-1"
               End If
           Catch
               Log("JSON parser error " & LastException.Message)
               restresult = "-1"
           End Try
       Else
           'there is no json
           restresult = "0"
       End If
   Else
       'not response
       restresult = "-2"
   End If
   J.release
End Sub
Of course I tried with resumablesub and wait for, but that isn't good also.
Can I make that kind of httpjob code where I can get the string result at once? I.e.:
res = CallSub("REST","Stores_Get")
thanks in advance
 
Top