Hello
I have problem with following code which is used for sending string from uC to Android app(using Tcp socket).
In Debug mode its working fine - in Release it generate error.
There is app code:
Main
TcpClient
It's debug console:
Logger connected to: HUAWEI HUAWEI CUN-L21
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: android.os.NetworkOnMainThreadException
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1656)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1180)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:600)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:42)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:242)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:406)
at b4a.example.tcpclient$ResumableSub_SendMessageByteArray.resume(tcpclient.java:224)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1654)
... 8 more
--------- beginning of system
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
tcpclient_sendmessagebytearray (java line: 170)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1180)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:600)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:42)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:242)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:406)
at b4a.example.tcpclient._sendmessagebytearray(tcpclient.java:170)
at b4a.example.tcpclient._sendmessagestring(tcpclient.java:185)
at b4a.example.main._client_connected(main.java:377)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1044)
at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:978)
at b4a.example.tcpclient._tcpsocket_connected(tcpclient.java:194)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
android.os.NetworkOnMainThreadException
Can you point me out what I'm doing wrong?
Thanks!
Przemek
I have problem with following code which is used for sending string from uC to Android app(using Tcp socket).
In Debug mode its working fine - in Release it generate error.
There is app code:
Main
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
#BridgeLogger: True
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private EMessage As EditText
Private EResponse As EditText
Private EAddress As EditText
Private EPort As EditText
Private Client As TcpClient
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("PSTCPLayout")
EResponse.Enabled = False
EResponse.TextColor = Colors.Green
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Client_Connected
Client.SendMessageString(EMessage.Text & Chr(13))
End Sub
Sub Client_ResponseReady
EResponse.Text = Client.GetStringResponse
End Sub
Sub BSend_Click
Dim s_address As String = EAddress.Text
Dim s_port As Int = EPort.Text
Client.Initialize(s_address, s_port, 5000)
Client.RegisterConnectedEvent(Me, "Client_Connected")
Client.RegisterResponseReadyEvent(Me, "Client_ResponseReady")
Client.Connect
End Sub
TcpClient
B4X:
'Class module
Sub Class_Globals
Private TCPSocket As Socket
Private server_address As String
Private server_port As Int
Private timeout As Int
Private response() As Byte
Private event_method_name As String
Private event_module_name As Object
Private last_error As String
Private responded_event_method_name As String
Private responded_event_module As Object
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(s_address As String, s_port As Int, s_timeout As Int)
server_address = s_address
server_port = s_port
timeout = s_timeout
TCPSocket.Initialize("TCPSocket")
End Sub
Public Sub RegisterConnectedEvent(target_module_name As Object, target_method_name As String)
event_method_name = target_method_name
event_module_name = target_module_name
End Sub
Public Sub RegisterResponseReadyEvent(target_module_name As Object, target_method_name As String)
responded_event_method_name = target_method_name
responded_event_module = target_module_name
End Sub
Public Sub Connect
TCPSocket.Connect(server_address, server_port, timeout)
End Sub
Public Sub Disconnect
If TCPSocket.Connected Then TCPSocket.Close
End Sub
Public Sub isConnected As Boolean
Return TCPSocket.Connected
End Sub
Private Sub TCPSocket_Connected (Successful As Boolean)
If Successful Then
CallSub(event_module_name, event_method_name)
End If
End Sub
Public Sub GetStringResponse As String
Dim cv As ByteConverter
Return cv.StringFromBytes(response, "UTF8")
End Sub
Public Sub GetArrayOfByteResponse As Byte()
Return response
End Sub
Public Sub SendMessageString(mess As String)
SendMessageByteArray(mess.GetBytes("UTF8"))
End Sub
Public Sub SendMessageByteArray(mess() As Byte)
Dim os As OutputStream
Dim in_s As InputStream
Dim buffer(1024) As Byte
'Dim bufor As String
os = TCPSocket.OutputStream
os.WriteBytes(mess, 0, mess.Length)
in_s = TCPSocket.InputStream
' Wait for data to arrive in the buffer, if no data defined timeout is reached
Dim start As Long = DateTime.now
Do Until (DateTime.now>start+timeout Or in_s.BytesAvailable > 0)
'DoEvents
'Sleep(2)
Loop
If in_s.BytesAvailable > 0 Then
in_s.ReadBytes(buffer, 0, in_s.BytesAvailable)
response = buffer
CallSub(responded_event_module, responded_event_method_name)
Else
last_error = "No response from server"
End If
End Sub
Public Sub GetLastError As String
Return last_error
End Sub
It's debug console:
Logger connected to: HUAWEI HUAWEI CUN-L21
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: android.os.NetworkOnMainThreadException
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1656)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1180)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:600)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:42)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:242)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:406)
at b4a.example.tcpclient$ResumableSub_SendMessageByteArray.resume(tcpclient.java:224)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1654)
... 8 more
--------- beginning of system
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
tcpclient_sendmessagebytearray (java line: 170)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1180)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:600)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:42)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:242)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:406)
at b4a.example.tcpclient._sendmessagebytearray(tcpclient.java:170)
at b4a.example.tcpclient._sendmessagestring(tcpclient.java:185)
at b4a.example.main._client_connected(main.java:377)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1044)
at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:978)
at b4a.example.tcpclient._tcpsocket_connected(tcpclient.java:194)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
android.os.NetworkOnMainThreadException
Can you point me out what I'm doing wrong?
Thanks!
Przemek