2

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
)

2 Answers 2

1

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

// annotations omitted
suspend fun getRepos() : List<User>
Sign up to request clarification or add additional context in comments.

2 Comments

without Call is still the same :( debugger doesn't go to line with println and okhttp instantly send "END GET" in logs
do you have the internet permission in the manifest?
0

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

        })
    }
}

2 Comments

I copied everything and is the same after that line: val call: Call<List<User>> = apiInterface.getRepos() <- here app stop and doing nothing, just END GET
@discCard It worked for me!. Just remove the suspend func and try again. And also check your internet connection before the execute the script!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.