1
0
mirror of https://github.com/square/okhttp.git synced 2025-04-19 07:42:15 +03:00
okhttp/okhttp-coroutines
renovate[bot] a51cfbf841
Update dependency com.diffplug.spotless:spotless-plugin-gradle to v7 (#8702)
* Update dependency com.diffplug.spotless:spotless-plugin-gradle to v7

* Reformat

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Jake Wharton <jw@squareup.com>
2025-03-19 15:25:20 -04:00
..
2022-03-06 15:31:31 +00:00

OkHttp Coroutines

Support for Kotlin clients using coroutines.

val call = client.newCall(request)

call.executeAsync().use { response ->
  withContext(Dispatchers.IO) {
    println(response.body?.string())
  }
}

This is implemented using suspendCancellableCoroutine but uses the standard Dispatcher in OkHttp. This means that by default Kotlin's Dispatchers are not used.

Cancellation if implemented sensibly in both directions. Cancelling a coroutine scope, will cancel the call. Cancelling a call, will throw a CancellationException but not cancel the scope if caught.