Kotlin Android Retrofit 2.6.0 with coroutines error handling

后端 未结 7 2037
粉色の甜心
粉色の甜心 2021-01-30 23:09

I am using Retrofit 2.6.0 with coroutines for my web service call. I am getting the API response properly with all response codes (success & error cases). My Issue is when I

7条回答
  •  鱼传尺愫
    2021-01-30 23:36

    you could do the following in your ViewModel to gracefully handle exceptions:

    viewModelScope.launch {
       kotlin.runCatching {
          withContext(Dispatchers.IO){
              ApiConnectionBridge.getHomeUiDetails(SharedPrefUtils.getAuthToken(context))
          }
    
      }.onSuccess { 
           // do something with success response
      }.onFailure{
          // do something on failure response
      }
    
    }
    

提交回复
热议问题