1

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 String), i get the unused bytes at the end of the String:

</s12:Envelope>À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?
4

1 回答 1

2

首先,我认为您需要按如下方式构造字符串:

String data = new String(packet.getData(), 
                         packet.getOffset(), 
                         packet.getLength());

至于有没有更好的方法。AIUI 不是真的,尽管可能有第三方 API 可以让填充/清空数据报更容易一些,因为所有字节打包都非常繁琐。

于 2010-05-17T13:30:01.867 回答