How to pause/resume downloading with okhttp in Android

前端 未结 2 1590
孤独总比滥情好
孤独总比滥情好 2021-02-05 21:43

I use okhttp library for download files in android. I download successfully. But something is wrong when I pause and resume download.

Response request = new Req         


        
2条回答
  •  时光说笑
    2021-02-05 22:00

    val call = client.newCall(request)
    
    call.enqueue(object : Callback {
        override fun onFailure(call: Call, e: IOException) {
            listener.onFail(e)
        }
    
        override fun onResponse(call: Call, response: Response) {
            // write the stream to a file (downloading happening)
            val stream = response.body?.byteStream()
        }
    })
    
    // this will stop the downloading
    call.cancel()
    

    to resume use the "Range" header with your request and append the stream to already downloaded file.

提交回复
热议问题