Skip to content

Instantly share code, notes, and snippets.

@adityaladwa
Last active November 20, 2020 09:57
Show Gist options
  • Select an option

  • Save adityaladwa/04a3246b5a6b33c5e271492a91d0c4ca to your computer and use it in GitHub Desktop.

Select an option

Save adityaladwa/04a3246b5a6b33c5e271492a91d0c4ca to your computer and use it in GitHub Desktop.

Revisions

  1. adityaladwa revised this gist Nov 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NetworkInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    class LiveNetworkInterceptor(context: Context) : Interceptor {
    class NetworkInterceptor(context: Context) : Interceptor {
    private val mApplicationContext: Context = context.applicationContext

    private val isConnected: Boolean
  2. adityaladwa revised this gist Nov 19, 2017. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions NetworkInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -7,16 +7,15 @@ class LiveNetworkInterceptor(context: Context) : Interceptor {
    val activeNetwork = cm.activeNetworkInfo
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting
    }

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
    val connected = isConnected
    val originalRequest = chain.request()
    if (!connected) {
    if (!isConnected) {
    throw NoNetworkException()
    }
    return chain.proceed(originalRequest)
    }

    class NoNetworkException internal constructor() : RuntimeException("Please check Network Connection")
    }
    }
  3. adityaladwa created this gist Nov 19, 2017.
    22 changes: 22 additions & 0 deletions NetworkInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    class LiveNetworkInterceptor(context: Context) : Interceptor {
    private val mApplicationContext: Context = context.applicationContext

    private val isConnected: Boolean
    get() {
    val cm = mApplicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetwork = cm.activeNetworkInfo
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting
    }

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
    val connected = isConnected
    val originalRequest = chain.request()
    if (!connected) {
    throw NoNetworkException()
    }
    return chain.proceed(originalRequest)
    }

    class NoNetworkException internal constructor() : RuntimeException("Please check Network Connection")
    }