在有效的网址字符串上获取“ java.net.MalformedURLException:无协议”异常?

时间:2018-08-20 23:54:03

标签: java android android-studio kotlin

我正在尝试用Kotlin编写一个Android应用程序,该应用程序可以通过网络下载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看起来不错,所以我无法弄清楚错误的原因。任何帮助表示赞赏。

0 个答案:

没有答案
相关问题