Android Question FOR (Loop) and Call a WebService | OkHttpUtils2

trejocode

Member
Licensed User
Hello, I have a problem is that I do not understand at all how to "Block" an HTTP request because I use Wait For (Resumable).
Look at the case:

I select my items from my local database, and then I send the data to an API but they are not sent properly because, as it is a "non-blocking" event, it overwrites the "current" values by the last one, I attach an example of what I have:

B4X:
Sub FirstStep
    'Get Data from SQLite
    SQL.BeginTransaction
    Try
        Cursor = SQL.ExecQuery("SELECT column1, column1 FROM table WHERE something = 0")
        For i = 0 To Cursor.RowCount -1
            Cursor.Position = i
            Dim Column1 As String = Cursor.GetInt("column1")
            Dim Column2 As String = Cursor.GetInt("column2")
            SendData(Column1, Column2)
        Next
        SQL.TransactionSuccessful
    Catch
        Log(LastException.Message)
    End Try
    SQL.EndTransaction
    
    
End Sub

Sub SendData(Column1 As Int, Column2 As Int)
    
    Dim Data As Map
    Data.Initialize
    Data.Put("data1", Column1)
    Data.Put("data2", Column2)
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Data)
    Dim JSONString As String
    JSONString = JSONGenerator.ToString
    HTTP.PostString("http://"&IP&":8081/petition", JSONString)
    HTTP.GetRequest.SetContentType("application/json")
    HTTP.GetRequest.Timeout = 30000
    Wait For (HTTP) JobDone(response As HttpJob)
    If response.Success Then
        Dim Successful As Boolean = response.Success
        ' If the Data are Inserted on the WebService THEN: UPDATE MY SQLite DB
        If Successful Then
            SQL.BeginTransaction
            Try
                SQL.ExecNonQuery2("UPDATE table SET status = 1 WHERE something = ?", Array As Object(Column1))
                SQL.TransactionSuccessful
            Catch
                Log(LastException.Message)
            End Try
            SQL.EndTransaction
        Else
            Log("Nope, ERROR")
        End If
    End If
    HTTP.Release
    
End Sub
What I would like is to find the way to do "Blocking the HTTP JOB" the event of sending to the API so that everything is inserted properly and not only insert the last one, or what would be the way to make it send the data of proper way?

Thank you
 
Last edited:
Top