TheGreatCornholio
TheGreatCornholio

Reputation: 1455

Android OkHttpClient - download file with Kotlin

I edit the question with the code I have so far now after helping from answers:

                val folder = File(context.cacheDir, "videos")

                val request: Request = Request.Builder().url(fileUrl).build()
                OkHttpClient().newCall(request).enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        // Method body omitted for brevity
                    }

                    override fun onResponse(call: Call, response: okhttp3.Response) {
                        if (!folder.exists()) {
                            val folderCreated: Boolean = folder.mkdir()
                        }
                        val file = File(folder.path.toString() + "/video.mp4")
                        
                        
                        val fileCreated: Boolean = file.createNewFile()
                        val sink: BufferedSink = Okio.buffer(Okio.sink(file))
                        sink.writeAll(response.body().source())
                        sink.close()
                    }
                })

I get

Using 'buffer(Sink): BufferedSink' is an error. moved to extension function

on

buffer in Okio.buffer

Using 'sink(File): Sink' is an error. moved to extension function

on

sink in Okio.sink

and

Using 'body(): ResponseBody?' is an error. moved to val

on

`body` in `response.body()`

Upvotes: 0

Views: 2746

Answers (2)

Jeroen Steenbeeke
Jeroen Steenbeeke

Reputation: 4013

There are a number of issues with your code. The first is that you need to remove the parentheses from your object expression, since Callback is an interface:

OkHttpClient().newCall(request).enqueue(object : Callback {

The second is that your method signatures are wrong. The parameters of onFailure and onSuccess are not nullable (so you can't use ?), and Response has no generic type:

override fun onFailure(call: Call, e: IOException) {
  // Method body omitted for brevity
}

override fun onResponse(call: Call, response: Response) {
  // Method body omitted for brevity
}

As for your error with sink, the version of OkHttp I'm using (4.9.1) has this method deprecated. The following logic with extension functions works for me:

val sink: BufferedSink = file.sink().buffer()

I also got a deprecation error on your call to sink.writeAll(response.body().source()), which I replaced with the following:

val body = response.body
                    
if (body != null) {
  val sink: BufferedSink = file.sink().buffer()
  sink.writeAll(body.source())
  sink.close()
}

However it might be better to do this check earlier to avoid creating a bunch of empty files.

Upvotes: 3

Dmytro Marchuk
Dmytro Marchuk

Reputation: 146

You need to modify the code a bit

val request: Request = Request.Builder().url(fileUrl).build()
OkHttpClient().newCall(request).enqueue(object : Callback {
    fun onFailure(call: Call, e: IOException) {

    }

    @Throws(IOException::class)
    fun onResponse(call: Call, response: Response) {
        if (!folder.exists()) {
            val folderCreated: Boolean = folder.mkdir()
        }
        val file = File(folder.getPath().toString() + "/video.mp4")
        if (file.exists()) {
            val fileDeleted: Boolean = file.delete()

        }
        val fileCreated: Boolean = file.createNewFile()
        val sink: BufferedSink = Okio.buffer(Okio.sink(file))
        sink.writeAll(response.body().source())
        sink.close()
    }
})

Upvotes: 1

Related Questions