Skip to content

Latest commit

 

History

History
60 lines (43 loc) · 1.59 KB

suspendresponseconverter.md

File metadata and controls

60 lines (43 loc) · 1.59 KB

!!! warning "SuspendResponseConverter is deprecated, use Converter.SuspendResponseConverter instead"

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

To change this, you need to use a SuspendResponseConverter, you add your own or use Flow or Call

You can add RequestConverter on your Ktorfit object.

ktorfit.responseConverter(FlowResponseConverter())

Flow

Ktorfit has support for Kotlin Flow. You need add the FlowResponseConverter() to your Ktorfit instance.

ktorfit.responseConverter(FlowResponseConverter())
@GET("comments")
fun getCommentsById(@Query("postId") postId: String): Flow<List<Comment>>

Then you can drop the suspend modifier and wrap your return type with Flow<>

Call

ktorfit.responseConverter(CallResponseConverter())
@GET("people/{id}/")
fun getPersonById(@Path("id") peopleId: Int): Call<People>
exampleApi.getPersonById(3).onExecute(object : Callback<People>{
    override fun onResponse(call: People, response: HttpResponse) {
        //Do something with Response
    }

    override fun onError(exception: Exception) {
        //Do something with exception
    }
})

You can use Call to receive the response in a Callback.

Your own

You can also add your own Converter. You just need to implement RequestConverter. Inside the converter you need to handle the conversion from suspend to your async code.

class MyOwnResponseConverter : SuspendResponseConverter {
   ...