I am developing an android app with kotlin MVVM pattern, the problem is I am fetching data from net and even I put an exception for times with no internet connection, even though the app always crashes with LOGCAT : Illegal exception : can not connect to ..... Any help with that please? I tried to add interceptor and after that catch this :
class NoConnectivityException () : IOException()
class ApiException() : IOException()
//and inside reopistory
//
try {
val fetchData =
retrofitInterface
.getAll()
.await()
_downloadedResponse.postValue(fetchData)
} catch (e: NoConnectivityException) {
} catch (a: ApiException) {
}
//my interceptor interface :
interface ConnectivityInteceptor : Interceptor
//my interceptor implementation :
class ConnectivityInteceptorImpl(
context: Context
) : ConnectivityInteceptor {
private val appContext = context.applicationContext
override fun intercept(chain: Interceptor.Chain): Response {
if (!isOnLine())
throw NoConnectivityException()
return chain.proceed(chain.request())
}
private fun isOnLine(): Boolean {
val connectivityManager = appContext.getSystemService(Context.CONNECTIVITY_SERVICE)
as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
return networkInfo != null && networkInfo.isConnected
}
}