android URLConnection.getInputStream协议异常

时间:2013-10-25 17:36:17

标签: android

我试图从网址下载音频文件,这是代码

try { 

            URL url = new URL(urll);
            File extStore = Environment.getExternalStorageDirectory();
            File file = new File(extStore, "disk.wav");

            long startTime = System.currentTimeMillis();
            Log.d("ImageManager", "download begining");
            Log.d("ImageManager", "download url:" + url);
            Log.d("ImageManager", "downloaded file name:" + "disk.wav");
             URLConnection ucon =  url.openConnection();
            ((HttpURLConnection) ucon).setRequestMethod("GET");
            ucon.setDoInput(false);
            ucon.connect();
            InputStream is =  ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            /* Convert the Bytes read to a String. */
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/disk.wav");
            mediaPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }

到达此行时:

InputStream is =  ucon.getInputStream();
它扔了:

java.net.ProtocolException: This protocol does not support input

我尝试过HttpURLConnection,但没有运气 任何人都知道发生了什么 谢谢所有

1 个答案:

答案 0 :(得分:-1)

尝试这样做:

URL myUrl = new URL(/*Your Url String*/);
HttpUrlConnection mCon = (HttpUrlConnection)mCon.openConnection();
InputStream in = mCon.connect();