Android Question Network library v1.10 - UDP

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
Hi, try to try out the send function with UDP, but the device throws me an error of this type

** Activity (main) Resume **
main_buttonsend_click (java line: 385)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:276)
at libcore.io.IoBridge.sendto(IoBridge.java:513)
at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184)
at java.net.DatagramSocket.send(DatagramSocket.java:305)
at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket.Send(SocketWrapper.java:289)
at imagineear.UDP.main._buttonsend_click(main.java:385)
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.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:5246)
at android.widget.TextView.performClick(TextView.java:10571)
at android.view.View$PerformClick.run(View.java:21200)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
android.os.NetworkOnMainThreadException
** Activity (main) Pause, UserClosed = false **


el codigo es este

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #Bridgelogger:true
   
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim UDPSocket1 As UDPSocket
    Private Server As ServerSocket
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.
    Dim ButtonSend As Button
    Dim EditData As EditText
    Dim EditDest As EditText
    Dim LabelIP As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Server.Initialize(5000, "Server")
   
    Dim IP As String
    If FirstTime Then
        UDPSocket1.Initialize("UDP",47513,8000)
       
        IP = UDPSocket1.Port'Server.GetMyip
    End If
    Activity.LoadLayout("udp")
    LabelIP.Text = IP
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Msgbox("Message received: " & msg, "")
End Sub
Sub ButtonSend_Click
    Dim Packet As UDPPacket
    Dim dest As String
    Dim data() As Byte
    data = EditData.text.GetBytes("UTF8")
    dest = EditDest.text
    Packet.Initialize(data,dest,47513)
    UDPSocket1.Send(Packet)
   
    ToastMessageShow("Message send = " &EditData.Text, False)
End Sub
 
Top