将datagramPacket转换为String

时间:2016-03-16 08:54:09

标签: android mdns

我想创建一个Android应用程序,可以捕获连接到我的网络的设备发送的MDNS广告。 我希望得到回复

NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age = seconds until advertisement expires
LOCATION: URL of the web page to advertise
NT: urn:physical-web-org:device:Basic:1
NTS: ssdp:alive
SERVER: OS/version UPnP/1.0 product/version
USN: advertisement UUID 

任何人都可以帮我转换上面的回复吗?

1 个答案:

答案 0 :(得分:1)

试试这段代码

buffer = new byte[1024]; 
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
buffer = packet.getData();
String packetAsString=new String(buffer, 0, packet.getLength());
Log.d("DatagramPacket","Received: "+packetAsString);

请参阅此链接Datagrampacket to String