Android Question Http communication in B4x Pages

I'm using b4x pages with Android version 4 and later.
From one page, I send data to a remote display via HTTP post.
Everything works in debug mode, but not in release mode.
The data doesn't seem to reach the display (which instead responds to other commands sent by a test application).

The AI suggested these functions, which I added to my b4xpage, but they only work if the apk is compiled in debug mode.
How can I write some simple code compatible with Android 4 and later to post to an IP/port?


B4X:
Private Sub InviaDatiAlDisplay(Testo As String)
    If ipDisplay = "" Then Return
    
    Dim url As String = "http://" & ipDisplay & ":" & portaDisplay & "/update"
    Dim Su As StringUtils
    
    If Testo = "" Then
        Testo = "§" & Main.M.GP_RemoteDisplayDefaultMessage
    End If
    
    Dim datiPost As String = "testo=" & Su.EncodeUrl(Testo, "UTF8")
    
    Try
        ' Impacchettiamo i parametri di questo specifico invio
        Dim parametri() As Object = Array(url, datiPost, ipDisplay, portaDisplay)
        
        ' Li inseriamo nella coda globale (Thread-Safe per la lettura immediata)
        CodaInviiDisplay.Add(parametri)
        
        Dim joContext As JavaObject
        joContext.InitializeContext
        
        ' Creiamo il runnable a vuoto (i parametri li leggeremo dalla coda)
        Dim runnable As Object = joContext.CreateEventFromUI("java.lang.Runnable", "DisplayThread", Null)
        
        Dim joThread As JavaObject
        joThread = joThread.InitializeNewInstance("java.lang.Thread", Array(runnable))
        joThread.RunMethod("start", Null)
        
    Catch
        Log("Errore avvio Thread Display: " & LastException.Message)
    End Try
End Sub

Private Sub DisplayThread_Event (MethodName As String, Args() As Object) As Object
    Try
        ' Preleviamo i primi parametri disponibili nella coda
        If CodaInviiDisplay.Size = 0 Then Return Null
        
        Dim parametri() As Object = CodaInviiDisplay.Get(0)
        CodaInviiDisplay.RemoveAt(0) ' Rimuoviamo subito per i prossimi thread
        
        Dim LocaleUrl As String   = parametri(0)
        Dim LocaleDati As String  = parametri(1)
        Dim LocaleIp As String    = parametri(2)
        Dim LocalePorta As String = parametri(3)
        
        ' --- APERTURA CONNESSIONE NATIVA HTTP IN BACKGROUND ---
        Dim jUrl As JavaObject
        jUrl = jUrl.InitializeNewInstance("java.net.URL", Array(LocaleUrl))
        
        Dim conn As JavaObject = jUrl.RunMethod("openConnection", Null)
        
        conn.RunMethod("setConnectTimeout", Array(1000))
        conn.RunMethod("setReadTimeout", Array(1000))
        conn.RunMethod("setUseCaches", Array(False))
        conn.RunMethod("setDoOutput", Array(True))
        conn.RunMethod("setDoInput", Array(True))
        conn.RunMethod("setRequestMethod", Array("POST"))
        
        Dim hostSpecifico As String = LocaleIp & ":" & LocalePorta
        conn.RunMethod("setRequestProperty", Array("Host", hostSpecifico))
        conn.RunMethod("setRequestProperty", Array("Content-Type", "application/x-www-form-urlencoded"))
        conn.RunMethod("setRequestProperty", Array("Connection", "keep-alive"))
        
        Dim bytes() As Byte = LocaleDati.GetBytes("UTF8")
        conn.RunMethod("setFixedLengthStreamingMode", Array(bytes.Length))
        
        Dim outStream As JavaObject = conn.RunMethod("getOutputStream", Null)
        outStream.RunMethod("write", Array(bytes))
        outStream.RunMethod("flush", Null)
        outStream.RunMethod("close", Null)
        
        Dim responseCode As Int = conn.RunMethod("getResponseCode", Null)
        Log("Risposta Display in BG: " & responseCode)
        
        ' Svuotamento buffer per Keep-Alive
        Dim inStream As JavaObject
        If responseCode >= 200 And responseCode < 300 Then
            inStream = conn.RunMethod("getInputStream", Null)
        Else
            inStream = conn.RunMethod("getErrorStream", Null)
        End If
        
        If inStream.IsInitialized And inStream <> Null Then
            Dim buffer(512) As Byte
            Dim read As Int = 1
            Do While read > 0
                read = inStream.RunMethod("read", Array(buffer))
            Loop
            inStream.RunMethod("close", Null)
        End If
        
    Catch
        Log("Errore durante l'invio in Background: " & LastException.Message)
    End Try
    
    Return Null
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…