0

我正在尝试从 url 下载音频文件,这是代码

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 但没有运气,任何人都知道正在发生的事情,谢谢大家

4

1 回答 1

-1

尝试这样做:

URL myUrl = new URL(/*Your Url String*/);
HttpUrlConnection mCon = (HttpUrlConnection)mCon.openConnection();
InputStream in = mCon.connect();
于 2013-10-25T17:55:22.373 回答