Android Question resumablesubs and debug/release app

akinnovation

Member
Licensed User
Longtime User
Hi,
I have an app with resumablesubs that crash when I compile it in release mode, but work fine if I use it compiled in debug mode !
crash log:
controllo_g$ResumableSub_Leggiscriviresume (java line: 145)
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1605)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:116)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:161)
    at anywheresoftware.b4a.objects.streams.File$OutputStreamWrapper.WriteBytes(File.java:534)
    at ak.otto.controllo_g$ResumableSub_Leggiscrivi.resume(controllo_g.java:145)
    at ak.otto.controllo_g._vvvvvvvvvvvvvvv5(controllo_g.java:107)
    at ak.otto.main$ResumableSub_GestisciTag.resume(main.java:2281)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1770)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:7864)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)

the source code of the function that crash
function that crash:
public Sub Leggiscrivi(stringa As String)As ResumableSub
    Dim o As OutputStream
    Dim b() As Byte = stringa.GetBytes("UTF-8")
    Log("leggiscivi: "&stringa)
    
    o=sckt.OutputStream
    o.WriteBytes(b,0,b.Length)
    o.Flush
 
Log("fineOut")
    Dim i As InputStream
    i=sckt.InputStream
 
    Dim buffer(1024) As Byte
    Dim count As Int = 0
    Dim length As Int = 0
        
    Dim risposta As String
    risposta="N"
    'response loop (need exit condition)
    Do Until count=-1
        'Log("Do until "&count)
        'you can also send here more
        length = i.BytesAvailable '?
        count = i.ReadBytes(buffer, 0, length)
        Sleep(0)
        If (length>0) Then
            Dim risposta As String
            Log("avail "&length)
            Log("count "&count)
            risposta=BytesToString(buffer,0,length,"UTF-8")
            Log("RISPOSTA "&risposta)
            count=-1
            'Return risposta
'            If risposta.StartsWith("S") Then
'                Main.Smile.Bitmap=LoadBitmap(File.DirAssets,"spuntaverde.png")
'                Main.Smile.invalidate
'                Main.vedismile(True)
'            Else
'                Main.Smile.Visible=False
'                Main.ora.Visible=True
'                Main.ora.SingleLine=False
'                Main.ora.Color=Colors.Red
'                Main.ora.TextColor=Colors.White
'                Main.ora.TextSize=12
'                Main.ora.Text="NON PUO' ENTRARE "&Chr(10)&Chr(13)&risposta.SubString2(2,risposta.IndexOf("#"))
            'End If
            'refresh.Enabled=True
        End If
        Log("Run")
    Loop
    Return risposta
End Sub

Where I call the sub
call to subs:
        If crs.RowCount>0 Then ' è un operatore
            crs.Position=0
            If parametri.controllo Then
                controllo_g1.Initialize("xx.xx.xx.xx",yyyyy) '(crs.GetString("codfisc"),crs.GetString("piva"),"abcd")
                wait for(controllo_g1.attendiInit) complete (result As Boolean)
                If result Then
                    Log("collegato")
                    
                    message= "?alfa?,"&crs.GetString("codfisc")&","&crs.GetString("piva")&","&"abcd"&Chr(10)&Chr(13)
                    wait for(controllo_g1.Leggiscrivi(message)) complete (risposta As String)
                    If risposta.StartsWith("S") Then
                        Smile.Bitmap=LoadBitmap(File.DirAssets,"spuntaverde.png")
                        Smile.invalidate
                        vedismile(True)
                    Else
                        Smile.Visible=False
                        ora.Visible=True
                        ora.SingleLine=False
                        ora.Color=Colors.Red
                        ora.TextColor=Colors.White
                        ora.TextSize=12
                        ora.Text="NON PUO' ENTRARE "&Chr(10)&Chr(13)&risposta.SubString2(2,risposta.IndexOf("#"))
                    End If
                    refresh.Enabled=True
                End If
            Else
                errore("Funzionalità non disponibile","ERR")
            End If
        Else
            'errore("Badge non riconosciuto", "ERR")
            'tag=false
            'uidp=""
            If s.LastIng(dbpath,0,Null,memoFilename) Then
                InOut="O"
            Else
                InOut="I"
            End If
            dati.AutoCausale=InOut&"-NR"

            'pazlbl.Text=uidp
            vedismile(True)
            dati.CodTag=uidp
            idattivo.Text=dati.CodTag
            'dati.Paz_Cant="Non Riconosciuto"
            dati.Paz_Cant=""
        End If
    End If
    crs.Close
    Return risposta
End Sub

Many thanks for your help
 

agraham

Expert
Licensed User
Longtime User
android.os.NetworkOnMainThreadException
This is the problem. You are doing network activity on the main thread and it's not allowed. Use AsyncStreams.

The reason why it works in debug is that then most of the code is running on the PC and not the device.
 
Upvote 1
Top