Datagrampacket to xml
Im trying to parse a Soap ProbeMatch message with XMLPullParser. I receive this via UDP Multicast. I use the following code to receive it.
byte[] buf = new byte[1900];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
mSocket.receive(packet);
// Damn ugly....
String data = new String(packet.getData())
If i convert the byte[] to String the Parser doesnt eat it... Are there any more elegant ways to do this?
When i print the xml (as开发者_如何学Python String), i get the unused bytes at the end of the String:
</s12:Envelope>À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?
First off, I think you'll want to construct the String as follows:
String data = new String(packet.getData(),
packet.getOffset(),
packet.getLength());
As to there being a better way. AIUI not really, though there might be a third party API out there that makes filling/emptying the datagrams a bit easier, as all the byte packing is very fiddly.
精彩评论