How to fix "inappropriate blocking method call" error in Kotlin Coroutines with OkHttp?
Hello, colleagues! I'm writing a Kotlin application using coroutines for asynchronous work. I use OkHttp for HTTP requests and Moshi for JSON parsing. Everything works, but I constantly see the warning "Inappropriate blocking method call" in IntelliJ IDEA on the `execute()` call of OkHttp. Here's my code:
```kotlin
suspend fun fetchData(): String {
val client = OkHttpClient()
val request = Request.Builder()
.url("https://api.example.com/data")
.build()
// Here IDE highlights execute() as a blocking call
val response = client.newCall(request).execute()
return response.body?.string() ?: ""
}
```
I understand that `execute()` is a blocking method that shouldn't be called inside a coroutine. But how do I properly make the request? I
No answers yet.
Log in to leave an answer