Home > Software design >  Unable to create call adapter for in Android
Unable to create call adapter for in Android

Time:02-05

I'm trying to fetch list of Client in my App. I made Ktor backend and put clients there. Im using Retrofit for fetching and Hilt for DI.

In my android app i made api:

@GET("/clients")
fun getAllClients(): List<Client>

In clientListRepoImpl:

override fun getAllClients(): List<Client> {
        return clientTrackerApi.getAllClients()
    }

In ViewModel I call interface clientRepository:

 init {
        viewModelScope.launch {
            val clients = clientsRepository.getAllClients() 
        }
    }

In listScreen:

val viewModel = hiltViewModel<ClientListViewModel>()
val myContext = LocalContext.current
LaunchedEffect(key1 = myContext) {
    viewModel.viewModelScope.launch {
        Log.d(TAG, "ClientsListScreen:")
    }
}

I also got this error:

java.lang.IllegalArgumentException: Unable to create call adapter for java.util.List<android.tvz.hr.clienttracker.data.domain.model.Client

I checked that ktor is returning Client e.g. of 1 Client:

[

    {
        "id": 1,
        "name": "Ronnie",
        "age": 23,
        "picture": "content://com.android.providers.media.documents/document/image:18",
        "aboutUser": "something"
    }

]

EDIT:

client model:

data class Client(
    val id: Int,
    val name: String,
    val age: Int,
    val picture: String? = null,
    val aboutUser: String? = null,
)

Im trying to fetch list of Client from my backend to show it in LazyList in Compose.

CodePudding user response:

You either need to make getAllClients() suspended function or return Call<List<Client>>. The second one will make your code look like this:

val call = clientsRepository.getAllClients()
call.enqueue(object: Callback<PlacesFoo>() {
      
      override fun onResponse(Call<List<Client>> call, 
                             Response<List<Client>>response) {
        
      }
      
      override fun onFailure(Call<List<Client>> call, Throwable t) {
        
      }
    })

Also use auto completion by android studio don't copy paste this, there could be a mistake.

  • Related