Android SHOUTcast 请求
我用 Java 编写了一些代码,用于解码 SHOUTcast 流并返回元数据。这段代码按预期工作,但是当我将其移植到 Android 时,相同的代码不起作用。具体来说,我似乎无法解析来自 SHOUTcast 服务器的 HTTP 响应标头。我可以在 Android 之外很好地解析它,但当通过 Android 发出请求时,我似乎除了垃圾之外什么也没有得到。
相关代码如下。
URL u = new URL("http://scfire-mtc-aa01.stream.aol.com:80/stream/1074");
URLConnection uc = u.openConnection();
uc.addRequestProperty("Icy-MetaData", "1");
InputStream in = uc.getInputStream();
byte[] byteheader = new byte[512];
int c = 0;
int i = 0;
int metaint = 0;
while ((c = in.read()) != -1){
byteheader[i] = (byte)c;
if (i > 4){
if (byteheader[i - 3] == '\r' &&
byteheader[i - 2] == '\n' &&
byteheader[i - 1] == '\r' &&
byteheader[i] == '\n')
break;
}
i++;
}
当在 Android 中运行时,此代码会溢出“byteheader”缓冲区。当在 Android 之外运行时,它可以正常运行。更奇怪的是,我通过 Wireshark 嗅探了对话,并将 Android 发送的标头回显到文件中。当使用 netcat 发出具有相同标头的请求时,我得到了适当的响应。当我查看 Android 的 logcat 输出时,“byteheader”仅包含垃圾。
我唯一的想法是,这是我忽视的环境问题。或者,我遗漏了一些非常明显的东西。
有什么想法吗?
作为编辑,我通过创建一个虚拟应用程序并仅在其中放入有问题的代码来进一步隔离问题。问题仍然存在,Android 返回垃圾,而我的相同外部代码按预期工作。我认为这可能与字符编码有关,但似乎两个环境都默认为 UTF8。
I have written some code in Java that will decode a SHOUTcast stream and return metadata. This code works as intended, but when I port it to Android the same code does not work. Specifically, I can't seem to parse the HTTP response header from the SHOUTcast server. Where I can parse it just fine outside Android, I seem to get nothing but garbage back when the request is made via Android.
The relevant code follows.
URL u = new URL("http://scfire-mtc-aa01.stream.aol.com:80/stream/1074");
URLConnection uc = u.openConnection();
uc.addRequestProperty("Icy-MetaData", "1");
InputStream in = uc.getInputStream();
byte[] byteheader = new byte[512];
int c = 0;
int i = 0;
int metaint = 0;
while ((c = in.read()) != -1){
byteheader[i] = (byte)c;
if (i > 4){
if (byteheader[i - 3] == '\r' &&
byteheader[i - 2] == '\n' &&
byteheader[i - 1] == '\r' &&
byteheader[i] == '\n')
break;
}
i++;
}
When run in Android, this code overflows the "byteheader" buffer. When run outside Android, it functions properly. To make matters stranger, I have sniffed on the conversation via Wireshark and echoed the header sent by Android to a file. When using netcat to make a request with the same header, I get the appropriate response back. When I look at the logcat output of Android, "byteheader" contains only garbage.
My only idea is that this is something environmental that I'm overlooking. Or, I'm missing something massively obvious.
Any ideas?
As an edit, I've further isolated the problem by creating a dummy app and putting only the offending code in it. The problem persists, with Android returning garbage, and my identical external code working as expected. I thought this could be somehow related to character encodings, but it appears both environments are defaulting to UTF8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用套接字连接,它应该可以工作。 Shoutcast 不提供通用 HTTP 连接。
Use a socket connection and it should work. Shoutcast does not provide a common HTTP connection.
我最终将问题追溯到 URLConnection。显然,该类在我的本地 JVM 上的运行方式与在 Android 上的运行方式不同。在 Android 之外,URLConnection 使响应标头保持不变。在 Android 中,响应标头被消耗,输入流从数据的第一个字节开始。标头信息可通过 getHeaderField 获得。
我不确定我是否理解这种行为差异,我只能将其归因于 Java 版本的差异。
I have finally tracked the problem down to URLConnection. Apparently the class operates differently on my local JVM than it does on Android. Outside Android, URLConnection leaves the response header intact. Within Android, the response header is consumed, and the input stream begins at the first byte of data. The header information is available via getHeaderField.
I'm not sure I understand this behavioral difference, and I can chalk it up only to a difference in Java versions.