OkHttp Interceptors in Android

May 25 2021 · Kotlin 1.4, Android 5, Android Studio 4.1

Part 1: Implementing OkHttp Interceptors

06. Implement an Analytics Interceptor

Episode complete

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Implement an Authorization Interceptor

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 06. Implement an Analytics Interceptor

Try creating an Interceptor that retries an API call if it fails just once.

Progressively, start moving network operations common to all API calls to an Interceptor.

To check the official documentation, head over to https://square.github.io/okhttp/interceptors/.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Intro

When releasing apps to production, it is helpful to have analytics data. Analytics can help you learn more about your users as well as narrow down the scope when debugging an issue.

class AnalyticsInterceptor(private val context: Context): Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {

    }
}
class AnalyticsInterceptor(private val context: Context): Interceptor {

    private val APP_VERSION = "X-App-Version"
    private val DEVICE_MODEL = "X-Device-Model"
    private val DEVICE_PLATFORM = "X-Device-Platform"
    private val OS_VERSION = "X-Device-OS-Version"

    override fun intercept(chain: Interceptor.Chain): Response {
        
    }
}
class AnalyticsInterceptor(private val context: Context): Interceptor {

    //.. headers

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val requestBuilder: Request.Builder = request.newBuilder()
    }
}
class AnalyticsInterceptor(private val context: Context): Interceptor {

    //.. headers

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val requestBuilder: Request.Builder = request.newBuilder()

        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        var version = packageInfo.versionName
    }
}
class AnalyticsInterceptor(private val context: Context): Interceptor {

    //.. headers

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val requestBuilder: Request.Builder = request.newBuilder()

        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        var version = packageInfo.versionName

        requestBuilder.addHeader(APP_VERSION, version)
    }
}
class AnalyticsInterceptor(private val context: Context): Interceptor {

    //.. headers

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val requestBuilder: Request.Builder = request.newBuilder()

        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        var version = packageInfo.versionName

        requestBuilder.addHeader(APP_VERSION, version)
        requestBuilder.addHeader(OS_VERSION, Build.VERSION.SDK_INT.toString())
    }
}
class AnalyticsInterceptor(private val context: Context): Interceptor {

    //.. headers

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val requestBuilder: Request.Builder = request.newBuilder()

        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        var version = packageInfo.versionName

        requestBuilder.addHeader(APP_VERSION, version)
        requestBuilder.addHeader(OS_VERSION, Build.VERSION.SDK_INT.toString())
        requestBuilder.addHeader(DEVICE_MODEL, Build.MODEL)
    }
}
class AnalyticsInterceptor(private val context: Context): Interceptor {

    //.. headers

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val requestBuilder: Request.Builder = request.newBuilder()

        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        var version = packageInfo.versionName

        requestBuilder.addHeader(APP_VERSION, version)
        requestBuilder.addHeader(OS_VERSION, Build.VERSION.SDK_INT.toString())
        requestBuilder.addHeader(DEVICE_MODEL, Build.MODEL)
        requestBuilder.addHeader(DEVICE_PLATFORM, "android")
        return chain.proceed(requestBuilder.build())
    }
}
val okHttpClient = OkHttpClient.Builder()
    .readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
    .connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
    .addInterceptor(ApiKeyInterceptor())
    .addInterceptor(AnalyticsInterceptor(context))
    .addInterceptor(loggingInterceptor)
    .build()
fun getOkHttpClient(context: Context): OkHttpClient {

}
fun getMovieApi(context: Context): MovieApi {
    if (movieApi == null) {
        movieApi = Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .client(OkHttpProvider.getOkHttpClient(context))
            .build()
            .create(MovieApi::class.java)
    }
    return movieApi!!
}