Handle network availability in Interceptor and cancel the request Kotlin Android

250 views Asked by At

In Android application, is it possible to check network availability in Interceptor. And only if the network is available, allow the request. If no network, cancel/abort the request.

Also, is it possible to get the network status without context

class ErrorInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        //Check and allow the request only if network connection is available
        val request: Request = chain.request()
        val response = chain.proceed(request)
        
        return response
    }

    private fun checkNetwork():Boolean{
       // Check network and return as per availability 
    }
}
0

There are 0 answers