Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HomeViewModel : ViewModel() {
- val text: LiveData<Event<String>> = liveData {
- emit(Event.Loading())
- try {
- emit(Event.Success(getData()!!))
- } catch(exception: Exception) {
- emit(Event.Failure(exception))
- }
- }
- private suspend fun getData() = withContext(Dispatchers.IO) {
- val client = OkHttpClient()
- val request = Request.Builder()
- .url("https://google.com")
- .build()
- val data = client.newCall(request).execute().body?.string()
- return@withContext data
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement