Network Example - NetworkOnMainThreadException

RKM904

Member
Licensed User
Longtime User
As a new user of b4a I have developed a couple of small apps and now I am attempting to develop a simple network app that will send a number (membership id) to a remote server and retrieve an associated number (golf handicap) from that server.

As a first step in my learning process I read the Network tutorial and downloaded the Network Example and attempted to run it.

The example fails on the line:
"sb.Append(tr.Readline) 'read at least one line "
with the error "android.os.NetworkOnMainThreadException"

My environment is:
Asus Transformer TF101 running Android version 4.0.3 (connected wireless to b4a)
Network Lib ver 1.21
Core Lib ver 2.01

no other Libraries included.

I have attempted, by browsing the forum threads and searching Google, to resolve the problem without success.

Any help (particularly a reference to a tutorial or other learning aid) would be much appreciated.

Code is below

HTML:
'Activity module
Sub Process_Globals
   Dim Socket1 As Socket
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

 Socket1.Initialize("Socket1")
 Socket1.Connect("nist1-ny.ustiming.org" , 13, 20000)
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

 Sub Socket1_Connected (Successful As Boolean)
 If Successful = False Then
   Msgbox(LastException.Message, "Error connecting")
   Return
 End If
 Dim tr As TextReader
 tr.Initialize(Socket1.InputStream)
 Dim sb As StringBuilder
 sb.Initialize
 sb.Append(tr.ReadLine) 'read at least one line    
 Do While tr.Ready        
   sb.Append(CRLF).Append(tr.ReadLine)    
 Loop    
 Msgbox("Time received: " & CRLF & sb.ToString, "")    
 Socket1.Close
 End Sub
 
Top