I am building a simple android app using jetpack compose with MVVVM architecture. For this I am using Retrofit2 for network request and Hilt for dependency injection. This App work fine while internet is available but when I turn off internet it crashes the App. So how to handle the network error while app is in offline mode ?
This is code for requesting data to API:
@Singleton
@Provides
fun provideRecipeService() : RecipeService {
return Retrofit.Builder()
.baseUrl("https://food2fork.ca/api/recipe/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RecipeService::class.java)
}
CodePudding user response:
it has nothing to do with your dependency injection, I think there is a uncatched exception in your code, so when your network call fails (throws Exception) you didn’t handle the exception, go to the network call and cover the whole network call with try-catch and test it, if it was ok, map the exception to error model and handle it in your ViewModel the way you want to treat your errors hope it helped