Instruction

Exploring Retrofit

Retrofit offers a clean and intuitive way to interact with web services in Android apps. It simplifies the process of making network requests, parsing responses, and handling errors. Retrofit leverages the power of annotations and interfaces to define the API endpoints, reducing boilerplate code and promoting a concise and readable implementation.

Comparing Retrofit to HttpsUrlConnection

Comparing Retrofit to the native HttpsUrlConnection class highlights its advantages in terms of simplicity, readability, and productivity.

  • Concise API Definition: Retrofit allows developers to define API endpoints as Java/Kotlin interfaces with annotated methods. These annotations specify the request type, URL, headers, query parameters, and request body. In contrast, using HttpsUrlConnection requires manual construction of the request and parsing of the response, leading to verbose code and increased chances of errors.

  • Type Safety: Retrofit generates type-safe HTTP clients based on the defined interfaces. This ensures compile-time verification of request parameters and response types, eliminating runtime errors caused by mismatched data types. With HttpsUrlConnection, you must handle type conversions manually.

  • Built-in Serialization: Retrofit seamlessly integrates with popular serialization libraries like Moshi or Kotlin Serialization to convert JSON responses into Kotlin objects. By specifying the expected response type in the interface method, Retrofit handles the deserialization automatically. In contrast, working with HttpsUrlConnection requires manual parsing of JSON responses, leading to repetitive and error-prone code. You’ll learn how to do this in a later lesson.

  • Built-in Threading: Retrofit supports threading out of the box, meaning you don’t have to switch threads manually or launch any coroutines to make API calls. This is done by using either execute() or enqueue(). You’ll learn more about this in the next lesson.

Exposing Retrofit’s Building Blocks

To build a Retrofit instance, you need at least three main parts:

  • Base URL: As the name suggests, this is the base part of the URL that will be the same for all API requests. You’ll append paths to the base URL to form full API endpoints.
  • An HTTP client instance—OkHttp: OkHttp is a widely used HTTP client library developed by Square for Java and Android apps. It simplifies making network requests and provides an easy-to-use API. You’ll almost exclusively use OkHttp with Retrofit in Android development. You won’t have to interact with OkHttp directly. Once you pass an instance to the Retrofit builder, Retrofit uses it automatically under the hood.
  • ApiService interface: This is a Kotlin/Java interface containing methods annotated with Retrofit annotations. Retrofit uses Java reflection APIs to generate implementations at runtime.

There are some other components you can attach to the Retrofit instance, but these are the most important ones, and you won’t need anything else in this lesson.

That’s a good amount of theory for now. It’s time to put this knowledge to good use. Proceed to the next section to start building the Retrofit instance. Continue where you left off in the previous lesson or download the starter project for this lesson.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo