Android Question NetworkOnMainThreadException

Cadenzo

Active Member
Licensed User
Longtime User
I am using the Network Library, sending something to the (Arduino) Server and wait for the respose in the same sub. In Debug mode it is working, in Release mode I get the NetworkOnMainThreadException error on this line:
tr_Server.ReadLine ' tr_Server is a TextReader .Initialize(Socket1.InputStream)

I also tried input_stream.ReadBytes, same result.

The communication is not http, just a view bytes on 192.168.4.1, port 1234, so I cannot use the OkHttp Lib, right?

Why has the Network Lib a event for UDP PacketArrived, but not for TCP?
 

Cadenzo

Active Member
Licensed User
Longtime User
Thank you for the fast reply. I tried the AsyncStreams, but AStream_NewData is never fired.
This I am doing:

- CallSub2(Starter, "Connect", sRequest) 'or CallSubDelayed2
- Socket1.Connect(host, port, 20000)
- in the Socket1_Connected event: AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
- Dim bBytes() As Byte = sText.GetBytes("UTF8") : AStreams.Write(bBytes)

But AStreams_NewData is not fired. Any idea, what I am doing wrong?

Update: The AStream_Error is fired. Now trying to get the reason ...
 
Last edited:
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
Some new strange thing:
I want to send two Bytes (".t") to Arduino (or NodeMCU) via WLAN
Textwriter with .Writeline and .Flush sends the right bytes
Even the Socket Output Stream with .WriteBytes sends it (Bytes from text.GetBytes("UTF8"))
But with astream.Write the recieved char array on the NodeMCU contains only strange things. Problem byte to char?? But it is all < 128

But the bigger problem is still the response from NodeMCU. Again, TextWriter works fine in debug mode, not in release. And with the AsyncStreams the AStream_Error is fired, not the AStreams_NewData :-(
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
OK, I think, I got it!
I have my own solution for data-management, so I could not use the astream.InitializePrefix, which sends the first 4 bytes for data-lenght.
Now I use astream.Initialize instead, and it seems to work :)

Thanks for the AsyncStreams suggestion.
SOLVED
 
Upvote 0
Top