android 使用UDP发送数据 DatagramSocket 创建对象为null

DatagramSocket socket=null;
try {
socket = new DatagramSocket();  //这里创建对象为空
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InetAddress serverAddress=null;
try {
serverAddress = InetAddress.getByName("192.168.1.101");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "hello";
byte data[] = str.getBytes();
DatagramPacket pkdata = new DatagramPacket (data , data.length , serverAddress , 8899);
try {
socket.send(pkdata);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

解决方法:

需要在AndroidManifest.xml文件中添加uses-permission: "android.permission.INTERNET"

 

posted on 2014-03-27 21:44  lvlv岁月流逝  阅读(628)  评论(0编辑  收藏  举报

导航