数据报包转xml

发布于 2024-09-02 00:37:47 字数 811 浏览 3 评论 0原文

我正在尝试使用 XMLPullParser 解析 Soap ProbeMatch 消息。我通过 UDP 多播收到此消息。我使用以下代码来接收它。

byte[] buf = new byte[1900];
DatagramPacket packet = new DatagramPacket(buf, buf.length);

mSocket.receive(packet);

// Damn ugly....
String data = new String(packet.getData())

如果我将 byte[] 转换为 String ,解析器不会吃掉它......有没有更优雅的方法来做到这一点?

当我打印 xml(作为字符串)时,我在字符串末尾得到未使用的字节:

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

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>À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?À?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2024-09-09 00:37:47

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文