Modern Android Mastery: From Zero to Play Store
Lecture 5

Connecting to the World: Data and Networking

Modern Android Mastery: From Zero to Play Store

Transcript

Your app loads. The user taps a button. Nothing happens. The screen freezes. Five seconds pass. Android can flag it as an ANR, an Application Not Responding error. The user force-quits. That is not a crash. That is a networking mistake. Since Android 3.0, performing network calls on the main thread throws a NetworkOnMainThreadException. The OS blocks it entirely. And even when developers work around that, doing heavy I/O on the wrong thread triggers ANR errors that Android's vitals system flags directly. The UI thread is sacred. Touch it with a network call, and the app dies. Networking logic is crucial in the data layer, where Retrofit and Room integrate to manage network calls and local data storage efficiently. Jetpack's recommended architecture places networking in repositories and data sources, exposing UI-ready data as flows or suspend functions that Compose screens observe as state. The separation you set up in lecture four is exactly what makes clean networking possible. Retrofit simplifies networking by converting HTTP APIs into Kotlin interfaces, allowing seamless integration with coroutines for efficient network operations. REST APIs use methods like GET, POST, PUT, and DELETE on URL-identified resources. Retrofit maps those directly. For example, a function annotated with GET and a path returns a data class populated automatically from the JSON response. Libraries like kotlinx.serialization, Moshi, or Gson handle that JSON conversion via Retrofit converters. No manual parsing. No boilerplate. Now, Retrofit integrates natively with Kotlin coroutines. You declare your API interface functions as suspend functions. That means a network call reads like sequential code, but runs asynchronously off the main thread. Coroutines provide structured concurrency through CoroutineScope and Job hierarchies, which prevents memory leaks and orphaned requests. The recommended pattern is viewModelScope. Work launched there is automatically canceled when the ViewModel is destroyed. That means no dangling network calls after the user navigates away, Adrià. Retrofit, powered by OkHttp, optimizes network efficiency with features like connection pooling and HTTP/2 support, crucial for mobile environments. OkHttp also supports connect, read, and write timeouts. That matters because unreliable networks are the norm on mobile. Retrofit surfaces failed calls as IOExceptions you catch and map to UI error state. One more thing: from Android 9 onward, all plain-text HTTP traffic is blocked by default. Production apps must use HTTPS. Offline-first apps leverage Retrofit and Room to ensure seamless data access, fetching from the network and caching locally for uninterrupted UI performance. The UI can keep working from local data instead of waiting on a live connection. For images, Adrià, the Coil library provides an AsyncImage composable that handles asynchronous loading, caching, and memory management, built on OkHttp and coroutines. And remember: network requests should not be performed directly inside composable functions. Use ViewModels or LaunchedEffect instead. [short pause] The takeaway is straightforward. Retrofit defines your API as a Kotlin interface. Coroutines with viewModelScope run calls safely off the main thread and cancel automatically. Room stores data locally so your app works without a connection. The Repository sits between all of it, giving ViewModels a single clean source of truth. Adrià, get those three pieces working together and your app stops being fragile. It becomes resilient. That is the data layer done right.