在我的安卓应用中:
应用程序/build.gradle
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation "com.squareup.retrofit2:converter-gson:2.6.0"
implementation "com.squareup.retrofit2:retrofit:2.6.0"
在我的界面中:
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
interface MyRestClient {
@GET("/event")
suspend fun getEvents(@Query("orgn") base: Int, @Query("event") quote: Int): Response<List<Event>>
}
我想同步调用这个方法。
所以我试试这个:
import retrofit2.Call
import retrofit2.Response
class TransportService {
companion object {
private val myRestClient =
RestClientFactory.createRestClient(MyRestClient ::class.java)
suspend fun getEventSync(orgn: Int, event: Int): Response<*> {
val call: Call<*> = myRestClient .getEvents(orgn, event)
return call.execute()
}
并以此调用:
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
viewModelScope.launch(Dispatchers.Main) {
val response = TransportService.getEvents(100, 200)
但我在这一行得到编译错误:
val call: Call<*> = myRestClient.getEvents(orgn, event)
错误是:
Type mismatch: inferred type is Response<List<Event>> but Call<*> was expected