URLConnection到mediaUrl失败,获取错误消息意外状态行:KitKat上的ICY 200 OK

时间:2014-02-21 12:19:43

标签: java android connect android-4.4-kitkat urlconnection

我已将AudioStreaming应用到我的应用程序中。所有代码运行良好,除了在Android 4.4.2上,它不起作用,我在我的应用程序上收到来自DDMS的错误消息:

Unable to initialize the MediaPlayer for fileUrl=http://live.radiorodja.com:80
java.net.ProtocolException: Unexpected status line: ICY 200 OK
E/info.wd.controller.StreamingMediaPlayer$1(28900): Unable to initialize the MediaPlayer for fileUrl=http://live.radiorodja.com:80
E/info.wd.controller.StreamingMediaPlayer$1(28900): java.net.ProtocolException: Unexpected status line: ICY 200 OK
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at com.android.okhttp.internal.http.RawHeaders.setStatusLine(RawHeaders.java:108)
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308)
E/info.wd.controller.controller.StreamingMediaPlayer$1(28900):  at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at info.wd.controller.StreamingMediaPlayer.downloadAudioIncrement(StreamingMediaPlayer.java:99)
E/info.wd.controller.StreamingMediaPlayer$1(28900):     at info.wd.controller.StreamingMediaPlayer$1.run(StreamingMediaPlayer.java:68)

这是我的downloadAudioIncrement源代码:

public void downloadAudioIncrement(String mediaUrl) throws IOException {
    isloading = true;
    URLConnection cn = new URL(mediaUrl).openConnection();

    //connecting
    cn.setReadTimeout(60000);
    cn.connect();
    statusPesan("", "Menghubungkan..");

    //loading
    InputStream stream = cn.getInputStream();
    statusPesan("", "Mohon tunggu...");

    if (stream == null) {
        error = true;
        statusPesan("", "Koneksi bermasalah");
        Log.e(getClass().getName(), "Unable to create InputStream for mediaUrl:" + mediaUrl);
    }

    downloadingMediaFile = new File(Environment.getExternalStorageDirectory()+ "/recradiodakwah/cache/", "streaming.dat");

    //ketika sudah ada, biasanya bakal crash, so delete yang sudah ada.
    if (downloadingMediaFile.exists()) {
        downloadingMediaFile.delete();
    }

    FileOutputStream out = new FileOutputStream(downloadingMediaFile);   
    byte buf[] = new byte[16384];
    int totalBytesRead = 0;
    setIncrementalBytesRead(0);

    do {
        int numread = stream.read(buf);   
        if (numread <= 0)   
            break;   
        out.write(buf, 0, numread);

        if (recordingStatus() == true) {
            recordStream.write(buf, 0, numread);
            totalKbrecord += numread;
            recordSizeMessage(String.format("%.2f",
                    ((float) (totalKbrecord / 1024) / 1024)) + " MB");
            mediaPlayer.setVolume(1, 1);
        } else {
            totalKbrecord = 0;
        }

        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying() == false) {
                mediaPlayer.seekTo(mediaPlayer.getCurrentPosition());
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        dur = mediaPlayer.getDuration();
                        mediaPlayer.start();
                    }
                });
            }
        }

        totalBytesRead += numread;
        setIncrementalBytesRead(getIncrementalBytesRead() + numread);
        totalKbRead = totalBytesRead/1000;

        if (totalKbRead < INTIAL_KB_BUFFER) {
            statusPesan("", "Persiapan...");
        } else {
            statusPesan(radioName, "[ " + String.format("%.2f", ((float) numread) / 128) + " kb ]");
            isloading = false;
        }

        testMediaBuffer();
    } while (validateNotInterrupted());   
        stream.close();
        out.close();
        statusPesan("", "");
    if (validateNotInterrupted()) {
        fireDataFullyLoaded();
    }
}  

StreamingMediaPlayer.java:99是cn.connect();

Android KitKat上的URLConnection是否有最佳实践并解决了这个问题? 谢谢。

1 个答案:

答案 0 :(得分:2)

明显的问题是您正在与之交谈的服务是发送不符合HTTP规范的HTTP响应。具体来说,它正在发送无效的状态行。有效的状态行如下所示:

HTTP/1.0 200 OK

HTTP/1.1 200 OK

我的结论是,您正在谈论的服务并非旨在使用HTTP协议进行访问。 URLConnection将不会处理此问题,也不会处理任何其他符合规范的HTTP库。

我认为您在这里遇到的是ICYCAST协议。

参考文献: