我正在尝试用kotlin编写一个android应用程序,通过web下载mp3文件。出现问题的代码如下:
fun downloadAudio(url: String, f: File) {
Log.d(JOURNAL_FETCHR_TAG, "URL is $url")
val iStream: InputStream = URL(url).openStream() // EXCEPTION ON THIS LINE
val oStream: OutputStream = FileOutputStream(f)
var buffer = ByteArray(1024)
var len = iStream.read(buffer)
while (len > 0) {
oStream.write(buffer, 0, len)
len = iStream.read(buffer)
}
oStream.close()
}
E/AndroidRuntime: FATAL EXCEPTION: IntentService[DownloadIntentService]
Process: com.sarpuner.journal, PID: 23301
java.net.MalformedURLException: no protocol: "http://telechargement.rfi.fr/rfi/francais/audio/jff/201808/journal_francais_facile_20h00_-_20h10_tu_20180819.mp3"
at java.net.URL.<init>(URL.java:601)
at java.net.URL.<init>(URL.java:498)
at java.net.URL.<init>(URL.java:447)
at com.sarpuner.journal.JournalFetchrKt.downloadAudio(JournalFetchr.kt:78)
at com.sarpuner.journal.DownloadIntentService.onHandleIntent(DownloadIntentService.kt:24)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:76)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:65)
不过,这个url看起来不错,所以我无法找出错误的原因。感谢您的帮助。