Professional Documents
Culture Documents
Alexander Nelson
November 17, 2021
University of Arkansas - Department of Computer Science and Computer Engineering
Java Interfaces
Java Interface
Example:
public interface GitHubService {
@GET(“users/user/repos”)
Call<List<Repo>>listRepos(@Path(“user”) String user);
}
Retrofit Instance
Example:
Call<List<Repo>> repos =
service.listRepos(“alexanderhnelson”);
Annotations
• HEAD
• GET
• POST
• PUT
• DELETE
Specifying Parameters
Example:
@GET(”group/{id}/users”)
Call<List<User>> groupList(@Path(“id”) int groupId);
When this method is called, creates a GET request with the
specified ID
Query Parameters
Example:
@GET(”group/id/users”)
Call<List<User>> groupList(@Path(“id”) int groupId,
@Query(“sort”) String sort);
Creates a GET at BASE URL/group/id/users?sort=(sort)
Creating Complex Queries
@GET(”group/id/users”)
Call<List<User>> groupList(@Path(“id”) int groupId,
@QueryMap Map<String, String> options);
Passing Objects
The HTTP Body can be specified using a Java Object with the
@Body tag
@POST(“users/new”)
Call<User> createUser(@Body User user);
Sending Form Encoded data
@FormUrlEncoded
@POST(“user/edit”)
Call<User> updateUser(@Field(“first name”) String first,
@Field(“last name”) String last);
Multipart Requests
Example:
@Multipart
@PUT(”user/photo”)
Call<User> updateUser(@Part(“photo”) RequestBody photo,
@Part(“description”) RequestBody description);
Manipulating Headers
Example:
@GET(“user”)
Call<User> getUser(@Header(“Authorization”) String
authorization);
Common Headers
https://github.com/square/okhttp/wiki/Interceptors
Retrofit Configuration