不能避免手动投射

时间:2019-11-09 17:43:19

标签: android kotlin retrofit2.6

改良版2.6。

 @GET("/event")
    suspend fun getEvents(@Query("orgn") base: Int, @Query("event") quote: Int): Response<List<Event>>

在使用中:

import retrofit2.Response

 suspend fun getEvents(
            orgn: Int,
            event: Int,
            isCustomtHandle: Boolean = false
        ): Response<*> {
           return waitressCallRestClient.getEvents(orgn, event)
        }

并在我的ViewModel中:

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope

 viewModelScope.launch(Dispatchers.Main) {
            val response = TransportService.getEvents(100, 200)
            if (response.isSuccessful) { 
                val eventList: List<Event> = response.body() as List<Event>

如您所见,我必须强制转换为List<Event> 是否可以避免手动投射?

1 个答案:

答案 0 :(得分:0)

以这种方式使用任何

@GET("/event")
suspend fun getEvents(@Query("orgn") base: Int, @Query("event") quote: Int): Response<List<Any>>

然后

val eventList: List<Any> = response.body()