Hi I am currently developing an android studio application with kotlin and receiving this issue when attempting a GET request . As below are my code stubs . Any help would be appreciated I am new to this language.
data class Post (
val id: Int,
val username: String,
val password: String,
val created_at: String,
val updated_at: String
)
class courseViewModelFactory(private val repository: Repository): ViewModelProvider.Factory { override fun <T : ViewModel?> create(modelClass: Class): T {
return courseViewModel(repository) as T
}
class courseViewModel(private val repository: Repository): ViewModel() {
val myResponse: MutableLiveData<Response<Post>> = MutableLiveData()
fun getPost(){
viewModelScope.launch {
val response = repository.getPost()
myResponse.value = response
}
}
}
class Repository {
suspend fun getPost(): Response<Post> {
return RetrofitInstance.api.getPost()
}
}
object RetrofitInstance {
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val api: SimpleApi by lazy {
retrofit.create(SimpleApi::class.java)
}
}
interface SimpleApi {
@GET("api/v1/users")
suspend fun getPost(): Response<Post>
}