Android Question REST / PostBytes / Wait For

asales

Expert
Licensed User
Longtime User
I have this code that send all the selected records to a REST server:
B4X:
Dim lst As List = GetSelectedIDs  'insert in the list all the  selected id's
    
For i = 0 To lst.Size - 1
    Dim m As Map
    m = DBUtils.ExecuteMap(Starter.SQL1, "SELECT name, city FROM profile WHERE id = " & lst.Get(i), Null)
        
    If (m <> Null) And (m.IsInitialized = True) Then
        Dim j As HttpJob
        j.Initialize("", Me)
            
        Dim objMap As Map: objMap.Initialize
            
        objMap.Put("name", m.Get("name"))
        objMap.Put("city", m.Get("city"))
        Dim objJSon As JSONGenerator: objJSon.Initialize(objMap)
            
        j.PostBytes("http://" & ip & ":8080/InsertRecord", objJSon.ToString.GetBytes("ISO-8859-1"))
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ToastMessageShow(m.Get("name") & " sended with success", False)
        End If
        j.Release
    End If
Next

I tested and works, but I don't know if is the best way to send records in batch.

Any advices are welcome.
 

XbNnX_507

Active Member
Licensed User
Longtime User
your code looks good...
This is another option
B4X:
    Dim lst As List = GetSelectedIDs  'insert in the list all the  selected id's
    Dim m As Map
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("SELECT name, city FROM profile WHERE id =?")
    For i = 0 To lst.Size - 1
        If i > 0 Then sb.Append(" or id =?")
    Next
        m = DBUtils.ExecuteMap(Starter.SQL1, sb.ToString, lst)
       
        If (m <> Null) And (m.IsInitialized = True) Then
            Dim j As HttpJob
            j.Initialize("", Me)
           
            Dim objJSon As JSONGenerator: objJSon.Initialize(m)
           
            j.PostBytes("http://" & ip & ":8080/InsertRecord", objJSon.ToString.GetBytes("ISO-8859-1"))
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                ToastMessageShow("sended with success", False)
            End If
            j.Release
        End If

unlike your code this one use one query and sends it in one batch, Not one by one.
 
Last edited:
Upvote 0
Top