A type-safe HTTP client for Android and Java.

Apache-2.0

Quick Start

Maven Coordinates
com.squareup.retrofit2:retrofit

Setup Steps

  1. Add the Retrofit dependency to your build.gradle file.
  2. Define an interface for your API with annotations.
  3. Create a Retrofit instance with a base URL and converter.
  4. Use the interface to make network calls.

Overview

Retrofit is a type-safe HTTP client for Android and Java developed by Square. It simplifies networking by turning your HTTP API into a Java interface.

Features

Type-safe HTTP requests
Support for various converters (Gson, Moshi, etc.)
Call adapters for RxJava, Coroutines, and more
Interceptor chain for request/response manipulation
Easy error handling and cancellation

Use Cases

Fetching data from RESTful APIs
Uploading and downloading files
Handling authentication with interceptors
Integrating with backend services in mobile apps

When Not to Use

For very simple HTTP requests where overhead is not needed
When using GraphQL (consider libraries like Apollo)
For low-level socket communication or protocols other than HTTP

Usage Examples

Define API Interface
interface GitHubService {
    @GET("users/{user}/repos")
    fun listRepos(@Path("user") user: String): Call<List<Repo>>
}

Define a GET request with a path parameter.

Create Retrofit Instance
val retrofit = Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build()
val service = retrofit.create(GitHubService::class.java)

Build a Retrofit instance with Gson converter and create service.

Common Pitfalls

  • Missing converter factory leading to serialization errors
  • Not handling network calls off the main thread to avoid ANRs
  • Incorrect base URL or endpoint paths
  • Forgetting to add call adapters for reactive programming

Integration

Works well with

OkHttpGsonMoshiRxJavaKotlin Coroutines
  • Retrofit uses OkHttp as the default HTTP client.
  • Converters must be explicitly added for data serialization.

Alternatives

Volley

Simpler and built-in for Android, but less type-safe and flexible.

Ktor Client

Kotlin-first and supports multiplatform, but may have a steeper learning curve.

Maintenance & Health

  • Latest version is not specified; check the repository for the most recent release.
  • Min SDK, compile SDK, and target SDK are project-specific and should be set in the app's build configuration.

Related libraries