Android Question android.os.NetworkOnMainThreadException

AHilberink

Well-Known Member
Licensed User
Longtime User
Hi,

I use the following code:
B4X:
Sub SendToPrinter(bestand As String, ip As String) As ResumableSub
    Try
        Dim s As Socket
        s.Initialize("s")
        s.Connect(ip, 9100, 3000)
    
        Wait For s_Connected (Success As Boolean)
        If Success = False Then Return False
        Dim out As OutputStream = s.OutputStream
        Dim In As InputStream = File.OpenInput(File.DirInternal, bestand)
        Dim data() As Byte = Bit.InputStreamToBytes(In)
        In.Close
        Log("Data: "&data.Length)
    
        out.WriteBytes(data, 0, data.Length)
        Log(14)
        out.Flush
        Log(15)

        Sleep(300)
        s.Close
        Log(16)

        Return True
    Catch
        Log(LastException.Message)
        Return False
    End Try
End Sub

I got the exception "android.os.NetworkOnMainThreadException" on "out.WriteBytes(data, 0, data.Length)". Data: 1618751 is filled.

Can someone help?

Kind regards,
André
 
Top