发送UDP广播 出现这个错误android.os.NetworkOnMainThreadException;at java.net
.DatagramSocket.send 怎么解决
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<script type="text/javascript">
document.addEventListener('plusready', function(){
var DatagramSocket = plus.android.importClass("java.net.DatagramSocket");
var DatagramPacket = plus.android.importClass("java.net.DatagramPacket");
var InetAddress = plus.android.importClass("java.net.InetAddress");
var String = plus.android.importClass("java.lang.String");
var udp = new DatagramSocket();
var data = new String("gujhgujghugyugyugyugyugyugyug").getBytes("gb2312");//发送中文需要指定编码
var packet = new DatagramPacket(data, data.length, InetAddress.getByName("255.255.255.255"), 1024);
var Runable= plus.android.implements("java.lang.Runnable",{"run":function(){
udp.send(packet);
}});
var Thread=plus.android.newObject("java.lang.Thread",Runable);
plus.android.invoke(Thread,'start');
})
</script>
</head>
<body>
<button onclick="senddata();">发送数据</button>
</body>
</html>
现在的代码
0 个回复