Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Generic Response Code #2

Open
L1f12bscs2109 opened this issue Jul 10, 2020 · 1 comment
Open

Missing Generic Response Code #2

L1f12bscs2109 opened this issue Jul 10, 2020 · 1 comment

Comments

@L1f12bscs2109
Copy link

How can we check status code of response? The code is very beautifully written but i need to know how to get the response code because in interface you are returning List not Response so if you want to check mainRespository.getUsers().code you can not do it. I need to check the response code whether it is HTTP_200 , HTTP_UNAUTHORIZED so on.
Help will be appreciated, thanks in advance :)

@L1f12bscs2109 L1f12bscs2109 changed the title Missing Generic Internet Checks and Response Code Missing Generic Response Code Jul 10, 2020
@c99rahul
Copy link

It can be easily doable with the help of HttpLoggingInterceptor. Here is a quick workaround for the RetrofitBuilder.kt:

object RetrofitBuilder {
   
   // Prepare the interceptor
    var intercepter = HttpLoggingInterceptor().apply {
        this.level = HttpLoggingInterceptor.Level.BODY
    }

   // Prepare the client
    private val client = OkHttpClient.Builder().apply {
        this.addInterceptor(intercepter)
    }.build()

    private fun getRetrofit(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(Config.BASE_URL)
            .client(client) // <-- Hook the client right here
            .addConverterFactory(GsonConverterFactory.create())
            .build() //Doesn't require the adapter
    }

    val apiService: ApiService = getRetrofit().create(ApiService::class.java)
}

Filter the Logcat to see logs of OkhttpClient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants