Home > Back-end >  Kotlin coroutines doesn't work with retrofit - no response
Kotlin coroutines doesn't work with retrofit - no response

Time:02-27

I'm trying to learn coroutines with retrofit and hilt. There is simple api github https://api.github.com/users/JakeWharton/repos But my code give in log:

D/OkHttp: --> GET https://api.github.com/users/JakeWharton/repos
D/OkHttp: --> END GET

without any reponse, despite the fact that using postman I get list with repos.

In my function loadData() debugger stop on the 1st lane, it doesn't come to println, something is wrong but don't know what.

my codes:

   @Provides
    fun provideGitHubService(retrofit: Retrofit): GitHubService{
        return retrofit.create(GitHubService::class.java)
    }

    @Provides
    fun provideOkHttpClient(): OkHttpClient {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        return OkHttpClient
            .Builder()
            .addInterceptor(loggingInterceptor)
            .build()
    }

    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("https://github.com") // don't know how to remove it but it will be override anyway
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build()
    }

private fun getRepos() {
    viewModelScope.launch {
        loadData()
    }
}

private suspend fun loadData() {
    val response = service.getRepos()
    println(response). //debugger doesn't come here
}

interface GitHubService {
@GET("https://api.github.com/users/JakeWharton/repos")
suspend fun getRepos() : Call<List<User>>
}

data class User(
  @SerializedName("name") val name: String
)

CodePudding user response:

You don't need Call when using suspend. Please update the getRepos to:

// annotations omitted
suspend fun getRepos() : List<User>

CodePudding user response:

I think you did something wrong in the instance of retrofit. try this.

class User {
    @Expose
    @SerializedName("name")
    var name: String? = null
}

interface GitHubService {

    @GET("users/JakeWharton/repos")
    suspend fun getRepos(): Call<List<User>>
}

fun provideGitHubService(retrofit: Retrofit): GitHubService{
    return retrofit.create(GitHubService::class.java)
}

class Example {

    private val TAG = "Example"

    /* OKHTTP CLIENT */
    private fun provideOkHttpClient(): OkHttpClient {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        return OkHttpClient
            .Builder()
            .addInterceptor(loggingInterceptor)
            .build()
    }

    /* RETROFIT INSTANCE */
    private fun provideRetrofit(): Retrofit {
        return Retrofit.Builder()
            .baseUrl("https://api.github.com/") // don't know how to remove it but it will be override anyway
            .addConverterFactory(GsonConverterFactory.create())
            .client(provideOkHttpClient())
            .build()
    }

    /* LOADING DATA */
    suspend fun loadData() {
        val apiInterface = provideGitHubService(provideRetrofit())
        val call: Call<List<User>> = apiInterface.getRepos()
        call.enqueue( object : Callback<List<User>> {
            override fun onResponse(call: Call<List<User>>, response: Response<List<User>>) {
                for (users in response.body()!!){
                    Log.e(TAG, "NAME: ${users.name}")
                }
            }

            override fun onFailure(call: Call<List<User>>, t: Throwable) {
                Log.e(TAG, "onFailure: ${t.message}")
            }

        })
    }
}
  • Related