我试图下载从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来检查它是否工作? –