Reputation: 21
When I upload large file and do another request to server it will wait until uploading finished. Can we prioritize okhttp
request so that while uploading and send another request, it will try to finish the later request first ?
Upvotes: 2
Views: 229
Reputation: 13498
OkHttp does not support stream prioritisation like you are asking for. But you can achieve this by avoiding head of line blocking, and combining with some throttling strategy.
If you look at RequestBody https://square.github.io/okhttp/3.x/okhttp/okhttp3/RequestBody.html
The simple implementation just writes the content from the file as fast as possible
/** Returns a new request body that transmits the content of this. */
@JvmStatic
@JvmName("create")
fun File.asRequestBody(contentType: MediaType? = null): RequestBody {
return object : RequestBody() {
override fun contentType() = contentType
override fun contentLength() = length()
override fun writeTo(sink: BufferedSink) {
source().use { source -> sink.writeAll(source) }
}
}
}
Instead you could use something like this answer to achieve this
https://stackoverflow.com/a/59883281/1542667
Upvotes: 1