Retrofit on Android: calling a REST API from Java

Retrofit is Square’s HTTP client for Android and the JVM. You describe an API as a Java (or Kotlin) interface with annotations such as @GET and @Path, and Retrofit generates the implementation at runtime. It sends requests through OkHttp and uses a converter (Gson, Moshi, Jackson, kotlinx.serialization and others) to turn JSON into your model classes.
Dependencies
Section titled “Dependencies”In your app module’s build.gradle:
implementation 'com.squareup.retrofit2:retrofit:3.0.0'implementation 'com.squareup.retrofit2:converter-gson:3.0.0'3.0.0 was the latest release on Maven Central when this post was updated. It moved the underlying OkHttp dependency from 3.14 to 4.12 but kept the same package names and API as 2.x, so the code below also works with 2.9.0, which the original post used.
Your app also needs network access in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />Defining the API
Section titled “Defining the API”Say the service returns users at GET /users/{id}. Model the response and the endpoint:
public class User { public int id; public String name;}import retrofit2.Call;import retrofit2.http.GET;import retrofit2.http.Path;
public interface UserService { @GET("users/{id}") Call<User> getUser(@Path("id") int id);}{id} in the path is replaced by the argument annotated with @Path("id"). Gson maps JSON fields to fields of the same name on User.
Building the client
Section titled “Building the client”import okhttp3.OkHttpClient;import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;
public final class ApiClient { private static final String BASE_URL = "https://api.example.com/"; private static Retrofit retrofit;
private ApiClient() {}
public static synchronized Retrofit getClient() { if (retrofit == null) { OkHttpClient okHttp = new OkHttpClient.Builder().build(); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(okHttp) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; }}The base URL must end with /, or baseUrl(...) throws an IllegalArgumentException. Relative paths in the interface (like users/{id}) are resolved against it.
Making a request
Section titled “Making a request”import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;
UserService userService = ApiClient.getClient().create(UserService.class);
userService.getUser(1).enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { if (response.isSuccessful()) { User user = response.body(); // Update the UI with user } else { // HTTP error such as 404 or 500: inspect response.code() } }
@Override public void onFailure(Call<User> call, Throwable t) { // No response: network error, timeout, or JSON that failed to parse }});enqueue runs the request on a background thread, and on Android the callbacks are delivered on the main thread, so you can touch views directly in them. execute() is the synchronous version; calling it on the main thread throws NetworkOnMainThreadException.
Note the split: a 404 or 500 still lands in onResponse with isSuccessful() false. onFailure is only for requests that produced no usable response.
In Kotlin you can declare the method as suspend fun getUser(@Path("id") id: Int): User (supported since Retrofit 2.6.0) and call it from a coroutine instead of using callbacks.
Practical tips
Section titled “Practical tips”- Share one
OkHttpClient. EachOkHttpClienthas its own connection pool and thread pool. Create one for the app and reuse it (or derive variants withokHttp.newBuilder(), which shares the pools). - Add auth headers with an interceptor. An OkHttp
Interceptorcan add anAuthorizationheader to every request instead of repeating@Headeron each method. - Enable caching in OkHttp. Retrofit doesn’t cache; pass an
okhttp3.CachetoOkHttpClient.Builder.cache(...)and OkHttp will honour the server’s cache headers. - Log while debugging.
HttpLoggingInterceptorfromcom.squareup.okhttp3:logging-interceptorprints requests and responses. Turn it off in release builds, since it can log tokens.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)