我試圖下載從URL的音頻文件,這裏是代碼安卓URLConnection.getInputStream協議異常
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的嘗試,但沒有運氣 任何一個有線索AB發生了什麼事 感謝所有
要設置做輸入到假'ucon.setDoInput(假)',然後試圖讀取inutstream ..請把它設置爲true'ucon.setDoInput(true);'。如果這仍然不起作用,你使用什麼樣的URL ..?它被設置爲true時爲 –
;我得到FileNotFoundException在 URLConnection ucon = url.openConnection(); –
你能分享你正在使用的網址嗎?你可以嘗試在瀏覽器上的URL來檢查它是否工作? –