Download binary file from OKHTTP

前端 未结 6 1830
不知归路
不知归路 2020-11-22 11:53

I am using OKHTTP client for networking in my android application.

This example shows how to upload binary file. I would like to know how to get inputstream of binary

6条回答
  •  旧时难觅i
    2020-11-22 12:33

    Kotlin version based on kiddouk answer

     val request = Request.Builder().url(url).build()
     val response = OkHttpClient().newCall(request).execute()
     val downloadedFile = File(cacheDir, filename)
     val sink: BufferedSink = downloadedFile.sink().buffer()
     sink.writeAll(response.body!!.source())
     sink.close()
    

提交回复
热议问题