Setting Up Retrofit
Retrofit simplifies making network calls in Android applications. Here's a step-by-step guide to set it up:
Add Retrofit as a dependency¶
- Open your project's
build.gradlefile (located in theappmodule). - Add the following dependencies in the
dependenciesblock:
dependencies {
// Other dependencies
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
2.9.0 with the latest stable version if needed (check Retrofit documentation for updates).
Sync your project with Gradle to download the libraries.
Maven
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>(insert latest version)</version>
</dependency>
Create a Retrofit Instance¶
You need a Retrofit object to make network requests. Here's how to create one:
In this example, you would use the generated GitHubService object:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
Call<List<Repo>> repos = service.listRepos("octocat");
This code creates a Retrofit instance with the base URL of the GitHub API. Then, it uses this instance to create a GitHubService object. Finally, you can call the listRepos method on the service, passing the username "octocat" as an argument. This initiates a network request to retrieve the list of repositories for that user.
Define an API Interface¶
Create an interface that defines your API endpoints. This interface acts as a contract for your network calls.
Here's an example:
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
This interface, GitHubService, defines a single method, listRepos. The @GET annotation tells Retrofit that this method makes a GET request.
The {user} part in the URL indicates a placeholder that will be replaced with a specific username when the call is made. The @Path("user") annotation links the user parameter in the method with the {user} placeholder.
Once you've defined the interface, Retrofit generates an object that implements it at runtime. This object handles all the heavy lifting of making the network request, converting the response data into your desired format (like a list of repositories in this case), and notifying you of any errors.
Key Features of Retrofit Annotations¶
- URL parameter replacement: You can use placeholders in the URL to represent dynamic parts, like the username in the example.
- Query parameter support: Annotations allow you to specify additional parameters that are appended to the URL as a query string.
- Object conversion: Retrofit can automatically convert objects to JSON or other data formats for request bodies and parse responses back into your desired Java classes.
- Multipart request body and file upload: Retrofit supports building complex requests with multiple parts, including file uploads.