Retrofit turns your REST API into a Java interface.
public interface GitHubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user); }
The RestAdapter
class generates an implementation of the GitHubService
interface.
RestAdapter restAdapter = new RestAdapter.Builder() .setServer("https://api.github.com") .build(); GitHubService service = restAdapter.create(GitHubService.class);
Each call on the generated GitHubService
makes an HTTP request to the remote webserver.
List<Repo> repos = service.listRepos("octocat");
Use annotations to describe the HTTP request:
Annotations on the interface methods and its parameters indicate how a request will be handled.
Every method must have an HTTP annotation that provides the request method and relative URL. There are five built-in annotations: GET
, POST
, PUT
, DELETE
, and HEAD
. The relative URL of the resource is specified in the annotation.
@GET("/users/list")
You can also specify query parameters in the URL.
@GET("/users/list?sort=desc")
A request URL can be updated dynamically using replacement blocks and parameters on the method. A replacement block is an alphanumeric string surrounded by {
and }
. A corresponding parameter must be annotated with @Path
using the same string.
@GET("/group/{id}/users") List<User> groupList(@Path("id") int groupId);
Query parameters can also be added.
@GET("/group/{id}/users") List<User> groupList(@Path("id") int groupId, @Query("sort") String sort);
An object can be specified for use as an HTTP request body with the @Body
annotation.
@POST("/users/new") void createUser(@Body User user, Callback<User> cb);
The object will also be converted using the RestAdapter
's converter.
Methods can also be declared to send form-encoded and multipart data.
Form-encoded data is sent when @FormUrlEncoded
is present on the method. Each key-value pair is annotated with @Field
containing the name and the object providing the value.
@FormUrlEncoded @POST("/user/edit") User updateUser(@Field("first_name") String first, @Field("last_name") String last);
Multipart requests are used when @Multipart
is present on the method. Parts are declared using the @Part
annotation.
@Multipart @PUT("/user/photo") User updateUser(@Part("photo") TypedFile photo, @Part("description") TypedString description);
Multipart parts use the RestAdapter
's converter or they can implement TypedOutput
to handle their own serialization.
Methods can be declared for either synchronous or asynchronous execution.
A method with a return type will be executed synchronously.
@GET("/user/{id}/photo") Photo getUserPhoto(@Path("id") int id);
Asynchronous execution requires the last parameter of the method be a Callback
.
@GET("/user/{id}/photo") void getUserPhoto(@Path("id") int id, Callback<Photo> cb);
On Android, callbacks will be executed on the main thread. For desktop applications callbacks will happen on the same thread that executed the HTTP request.
Retrofit also integrates RxJava to support methods with a return type of rx.Observable
@GET("/user/{id}/photo") Observable<Photo> getUserPhoto(@Path("id") int id);
Observable requests are subscribed asynchronously and observed on the same thread that executed the HTTP request. To observe on a different thread (e.g. Android's main thread) call observeOn(Scheduler)
on the returned Observable
.
HTTP responses are automatically converted to a specified type using the RestAdapter
's converter which defaults to JSON. The desired type is declared as the method return type or using the Callback
or Observable
.
@GET("/users/list") List<User> userList(); @GET("/users/list") void userList(Callback<List<User>> cb); @GET("/users/list") Observable<List<User>> userList();
For access to the raw HTTP response use the Response
type.
@GET("/users/list") Response userList(); @GET("/users/list") void userList(Callback<Response> cb); @GET("/users/list") Observable<Response> userList();
The source code to the Retrofit, its samples, and this website is available on GitHub.
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>(insert latest version)</version>
</dependency>
If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.
When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. Please also make sure your code compiles by running mvn clean verify
.
Before your code can be accepted into the project you must also sign the Individual Contributor License Agreement (CLA).
Copyright 2013 Square, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.