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 に答える