0

In my android app:

app/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"

In my interface:

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>>
}

And I want to call this method synchronously.

So I try this:

    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()
                }

And call by this:

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)

But I get compile error in this line:

val call: Call<*> = myRestClient.getEvents(orgn, event)   

error is:

Type mismatch: inferred type is Response<List<Event>> but Call<*> was expected
Alexei
  • 14,350
  • 37
  • 121
  • 240

3 Answers3

1

The error message is telling you the problem.

val call: Call<*> = myRestClient.getEvents(orgn, event)   

getEvents returns Response<List<Event>> but you are trying to assign it to Call<*>.

With retrofit 2.6 and Coroutines you no longer need Call.

The getEventSync function no longer makes sense. You can decide between sync and async at the invocation spot. For blocking:

val events: Reponse<List<Event>> = runBlocking {
        myRestClient.getEvents(orgn, event)
}
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
0

You're returning a Response type in your MyRestClient interface, but expecting a Call in your TransportService.

In your TransportService class, change the parameter type of val call from Call<*> to Response<List<Event>>.

Also, since you want to execute this method synchronously, you probably don't need the suspend modifier on your getEvents method.

harold_admin
  • 1,117
  • 2
  • 10
  • 20
0

Here my solution:

 viewModelScope.launch(Dispatchers.Main) {
            Debug.d(TAG, "step_1")
            TransportService.getEvents(100, 200)
            Debug.d(TAG, "step_2")
            TransportService.getEvents(300, 400)
            Debug.d(TAG, "step_3")
            TransportService.getEvents(500, 600)
            Debug.d(TAG, "step_4")
        }

in TransportService.kt

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

In interface:

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

Because I use retrofit 2.6 and suspend function as result step_2 print only after finish TransportService.getEvents(100, 200). And step_2 print only after finish TransportService.getEvents(300, 400) and so on.

So this is what I need. I has synchronously call of getEvents()

Alexei
  • 14,350
  • 37
  • 121
  • 240