0

In my android app:

Here my interface method:

import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

@GET("traders/json")
suspend fun getTraidersList(): Response<List<Trader>>

Nice.

but I need to add 2 my custom properties to retrofit2.Response

e.g.

val isCorrect  : boolean
val myCustom : MyCustomClass

I want to set/get this properties. Smt like this:

val response: Response<List<Trader>> = TransportService.getTraidersList()
if (response.isCorrect) {
 // do some logic
}
val myCustom = response.getMyCustom()

Is is possible in Kotlin?

Alexei
  • 14,350
  • 37
  • 121
  • 240

1 Answers1

0

Only you can do in Kotlin is to add some extension members, which are in fact just a usual Java's static methods. All the stuff around extension getters and setters is also emulated using static methods.

Based on mentioned above we cannot add new state (fields) using static methods.

But what can we do (I'm not familiar with Retrofit, it should be possible), is to use extension getter isCorrect, which can read response status, and if it is 4xx or 5xx it returns false

Max Farsikov
  • 2,451
  • 19
  • 25