Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.59 KB | None | 0 0
  1. class HomeViewModel : ViewModel() {
  2.  
  3.     val text: LiveData<Event<String>> = liveData {
  4.         emit(Event.Loading())
  5.         try {
  6.             emit(Event.Success(getData()!!))
  7.         } catch(exception: Exception) {
  8.             emit(Event.Failure(exception))
  9.         }
  10.     }
  11.  
  12.     private suspend fun getData() = withContext(Dispatchers.IO) {
  13.         val client = OkHttpClient()
  14.         val request = Request.Builder()
  15.             .url("https://google.com")
  16.             .build()
  17.         val data = client.newCall(request).execute().body?.string()
  18.         return@withContext data
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement