Android Question Thread Exception

ferpahud

Active Member
Licensed User
Hello good day,

I'm trying to load some data from a table to a database online.

When I try to do this I get the following error:
"An error has occurred in sub:sincronizar_hc_responsesuccess
android.os.NetworkOnMainThreadException"

This is the code that loads data:

B4X:
Sub btnCargarPedidos_Click
    Dim req As HttpRequest
    Dim Query As String
    Dim id As Int
    Dim fecha As String
    Dim idCliente As Int
    Dim usuarioEmpleado As String
    Dim estado As String

    c=s.ExecQuery("SELECT * FROM pedido")
   
    If c.RowCount>0 Then
    For i=0 To c.RowCount-1
        c.Position=i
       
        id=c.GetString("id")
        fecha=c.GetString("fecha")
        idCliente=c.GetString("idCliente")
        usuarioEmpleado=c.GetString("usuarioEmpleado")
        estado=c.GetString("estado")
       
        Query="INSERT INTO Pedido (id, fecha, idCliente, usuarioEmpleado, estado) VALUES ('" & id & "','" & fecha & "','" & idCliente & "','" & usuarioEmpleado & "','" & estado & "')"
        req.InitializePost2("http://167.250.5.13/~distribuidora/paises.php", Query.GetBytes("UTF8"))
        hc.Execute(req, 1)
    Next
    End If
End Sub

Someone could help me? Thank you
 

ferpahud

Active Member
Licensed User
Where is the call to Job.PostString ?
instead of this:
B4X:
req.Initialize("http://167.250.5.13/paises.php", Query.GetBytes("UTF8"))
this??:
B4X:
req.PostString("http://167.250.5.13/paises.php", Query.GetBytes("UTF8"))
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

ferpahud

Active Member
Licensed User
B4X:
Sub btnCargarPedidos_Click
    Dim Query As String
    Dim id As Int
    Dim fecha As String
    Dim idCliente As Int
    Dim usuarioEmpleado As String
    Dim estado As String

    c=s.ExecQuery("SELECT * FROM pedido")
   
    If c.RowCount>0 Then
    For i=0 To c.RowCount-1
        c.Position=i
        Dim hc As HttpJob
        Dim req As HttpJob
        hc.Initialize("hc", Me)
        req.Initialize("req", Me)
        id=c.GetString("id")
        fecha=c.GetString("fecha")
        idCliente=c.GetString("idCliente")
        usuarioEmpleado=c.GetString("usuarioEmpleado")
        estado=c.GetString("estado")
       
        Query="INSERT INTO Pedido (id, fecha, idCliente, usuarioEmpleado, estado) VALUES ('" & id & "','" & fecha & "','" & idCliente & "','" & usuarioEmpleado & "','" & estado & "')"
        req.PostString("http://167.250.5.13/paises.php", Query.GetBytes("UTF8"))
    Next
    End If
End Sub

Sub JobDone (req As HttpJob)
   Log("JobName = " & req.JobName & ", Success = " & req.Success)
   If req.Success = True Then
      Select req.JobName
         Case "Job1", "Job2"
            'print the result to the logs
            Log(req.GetString)
         Case "Job3"
            'show the downloaded image
            Activity.SetBackgroundImage(req.GetBitmap)
      End Select
   Else
      Log("Error: " & req.ErrorMessage)
      ToastMessageShow("Error: " & req.ErrorMessage, True)
   End If
    req.Release
End Sub

error in:
B4X:
req.PostString("http://167.250.5.13/paises.php", Query.GetBytes("UTF8"))
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. ERROR IN
is NOT a valid errormessage! What error exactly do you get???

2. why are you creating two jobs where you need just one?

B4X:
   Dim hc As HttpJob
        Dim req As HttpJob
        hc.Initialize("hc", Me)
        req.Initialize("req", Me)

btw: The Jobname from the job aou are really sending is "req".
NOT Job1, NOT Job2 and even NOT Job3.

I suggest to learn how to work with httputils first and then try to build a app out of your knowledge then

I´m out here
 
Upvote 0

ferpahud

Active Member
Licensed User
1. ERROR IN
is NOT a valid errormessage! What error exactly do you get???

2. why are you creating two jobs where you need just one?

B4X:
   Dim hc As HttpJob
        Dim req As HttpJob
        hc.Initialize("hc", Me)
        req.Initialize("req", Me)

btw: The Jobname from the job aou are really sending is "req".
NOT Job1, NOT Job2 and even NOT Job3.

I suggest to learn how to work with httputils first and then try to build a app out of your knowledge then

I´m out here

Error is: "Object converted to String. This is probably a programming mistake (warning #7)"

try this:
B4X:
req.PostString("http://167.250.5.13/paises.php", Query)

but it does not work....
 
Upvote 0
Top