Share
Embed
Simple HTTP with Retrofit 2 (Droidcon NYC 2015)
Retrofit has been simplifying HTTP calls for years and this new version is no different. In addition to fixing some long-standing annoyances, there are a handful of new features which make it more powerful than ever.
This talk will focus on how the new APIs in Retrofit aid in making HTTP calls as simple as possible for your app. The integration with OkHttp and Okio APIs will be covered to ensure a full understanding of the HTTP stack. Common use-cases and design patterns through more advanced functionality will end the talk.
No prior Retrofit, OkHttp, or Okio exposure needed. The content may quickly cover or skip some introductory aspects in order to allow focus on more actionable and useful content.
Video: https://youtu.be/KIAoQbAu3eA
Jake Wharton PRO
August 27, 2015
Transcript
213 slides
-
Retrofit 2 will be out by the end of this
year " " — Naïve Man -
Retrofit 2 will be out by the end of this
year " " — Jake Wharton -
Retrofit 2 will be out by the end of this
year " " Droidcon NYC 2014 — Jake Wharton -
Retrofit 2 will be out by the end of this
year " " Droidcon NYC 2015 — Jake Wharton -
Retrofit 1 • Made open source on October 13th, 2010
• Originally a Bob Lee joint -
Retrofit 1 • Made open source on October 13th, 2010
• Originally a Bob Lee joint • Took over / stole stewardship mid-2012 -
Retrofit 1 • Made open source on October 13th, 2010
• Originally a Bob Lee joint • Took over / stole stewardship mid-2012 • Released 1.0 on May 13th, 2013 -
Retrofit 1 • Made open source on October 13th, 2010
• Originally a Bob Lee joint • Took over / stole stewardship mid-2012 • Released 1.0 on May 13th, 2013 • 18 releases post-1.0 with tons of features / fixes -
The Good • Interface service declarations • Method and parameter
annotations customize request -
Interface and annotations interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> repoContributors( @Path("owner")
String owner, @Path("repo") String repo); } -
The Good • Interface service declarations • Method and parameter
annotations customize request • Pluggable HTTP client and serialization -
Pluggable client and serialization builder.setClient(new UrlConnectionClient()); builder.setClient(new ApacheClient()); builder.setClient(new OkClient());
builder.setClient(new CustomClient()); -
Pluggable client and serialization interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } -
Pluggable client and serialization interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } builder.setConverter(new GsonConverter()); builder.setConverter(new JacksonConverter()); -
Pluggable client and serialization interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") ContributorResponse repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } -
Pluggable client and serialization interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") ContributorResponse repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } builder.setConverter(new ProtoConverter()); builder.setConverter(new WireConverter()); -
Pluggable client and serialization interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") ContributorResponse repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } builder.setConverter(new ProtoConverter()); builder.setConverter(new WireConverter()); builder.setConverter(new SimpleXMLConverter()); -
Pluggable client and serialization interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") ContributorResponse repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } builder.setConverter(new ProtoConverter()); builder.setConverter(new WireConverter()); builder.setConverter(new SimpleXMLConverter()); builder.setConverter(new CustomConverter()); -
The Good • Interface service declarations • Method and parameter
annotations customize request • Pluggable HTTP client and serialization • Synchronous, asynchronous, and RxJava execution -
Sync, async, and RxJava interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } List<Contributor> contributors = gitHubService.repoContributors("square", "retrofit"); -
Sync, async, and RxJava interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") void repoContributors(
@Path("owner") String owner, @Path("repo") String repo, Callback<List<Contributor>> cb); } service.repoContributors("square", "retrofit", new Callback<List<Contributor>>() { @Override void success(List<Contributor> contributors, Response response) { // ... } @Override void failure(RetrofitError error) { // ... } }); -
Sync, async, and RxJava interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> repoContributors(
@Path("owner") String owner, @Path("repo") String repo); } gitHubService.repoContributors("square", "retrofit") .subscribe(new Action1<List<Contributor>>() { @Override public void call(List<Contributor> contributors) { // ... } }); -
The Good • Interface service declarations • Method and parameter
annotations customize request • Pluggable HTTP client and serialization • Synchronous, asynchronous, and RxJava execution -
The Not-So-Good • Request/Response (and friends) model classes • No
access to serialized body and response data -
No access to body and response interface GitHubService { @GET("/repos/{owner}/{repo}/contributors")
List<Contributor> repoContributors( @Path("owner") String owner, @Path("repo") String repo); }X -
No access to body and response interface GitHubService { @GET("/repos/{owner}/{repo}/contributors")
List<Contributor> repoContributors( @Path("owner") String owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") Response repoContributors2( @Path("owner") String owner, @Path("repo") String repo); }X -
The Not-So-Good • Request/Response (and friends) model classes • No
access to serialized body and response data • Rigid execution mechanisms -
Rigid execution mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> repoContributors( @Path("owner")
String owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") void repoContributors( @Path("owner") String owner, @Path("repo") String repo, Callback<List<Contributor>> cb); } -
Rigid execution mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> repoContributors( @Path("owner")
String owner, @Path("repo") String repo); } -
Rigid execution mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") ListenableFuture<List<Contributor>> repoContributors( @Path("owner")
String owner, @Path("repo") String repo); } -
Rigid execution mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") CompletableFuture<List<Contributor>> repoContributors( @Path("owner")
String owner, @Path("repo") String repo); } -
The Not-So-Good • Request/Response (and friends) model classes • No
access to serialized body and response data • Rigid execution mechanisms • Inefficient converter usage -
Inefficient converter usage interface Converter { Object fromBody(TypedInput body, Type
type); TypedOutput toBody(Object object); } -
Inefficient converter usage interface Converter { Object fromBody(TypedInput body, Type
type); TypedOutput toBody(Object object); } Type TypeAdapter TypedInput/Output JsonAdapter ObjectReader/Writer Parser ProtoAdapter -
Inefficient converter usage interface Converter { Object fromBody(TypedInput body, Type
type); TypedOutput toBody(Object object); } Type TypeAdapter TypedInput/Output JsonAdapter ObjectReader/Writer Parser ProtoAdapter -
The Not-So-Good • Request/Response (and friends) model classes • No
access to serialized body and response data • Rigid execution mechanisms • Inefficient converter usage • Simple custom parameter types support -
Simple custom parameter types interface GitHubService { @GET("/search/repositories") RepositoriesResponse searchRepos(
@Query("q") String query, @Query("since") Date since); }X -
Simple custom parameter types interface GitHubService { @GET("/search/repositories") RepositoriesResponse searchRepos(
@Query("q") String query, @Query("since") Date since); }X /search/repositories?q=retrofit&since=2015-08-27 -
Simple custom parameter types interface GitHubService { @GET("/search/repositories") RepositoriesResponse searchRepos(
@Query("q") String query, @Query("since") Date since); }X /search/repositories?q=retrofit&since=2015-08-27 /search/repositories?q=retrofit&since=20150827 -
The Not-So-Good • Request/Response (and friends) model classes • No
access to serialized body and response data • Rigid execution mechanisms • Inefficient converter usage • Simple custom parameter types support -
Call • Models a single request/response pair • Separates request
creation from response handling -
Call • Models a single request/response pair • Separates request
creation from response handling • Each instance can only be used once... -
Call • Models a single request/response pair • Separates request
creation from response handling • Each instance can only be used once... • ...instances can be cloned -
Call • Models a single request/response pair • Separates request
creation from response handling • Each instance can only be used once... • ...instances can be cloned • Supports both synchronous and asynchronous execution. -
Call • Models a single request/response pair • Separates request
creation from response handling • Each instance can only be used once... • ...instances can be cloned • Supports both synchronous and asynchronous execution. • Can be (actually) canceled -
Call interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner,
@Path("repo") String repo); } Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); -
Call Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); response = call.execute();
// This will throw IllegalStateException: response = call.execute(); -
Call Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); response = call.execute();
// This will throw IllegalStateException: response = call.execute(); Call<List<Contributor>> call2 = call.clone(); // This will not throw: response = call2.execute(); -
Call Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); call.enqueue(new Callback<List<Contributor>>() {
@Override void onResponse(/* ... */) { // ... } @Override void onFailure(Throwable t) { // ... } }); -
Call Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); call.enqueue( ); // or...
call.execute(); // later... call.cancel(); -
Response Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); call.enqueue(new Callback<List<Contributor>>() { @Override
void onResponse(/* ... */) { // ... }X @Override void failure(Throwable t) { // ... }X }); -
Response Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); call.enqueue(new Callback<List<Contributor>>() { @Override
void onResponse(Response<List<Contributor>> response) { // ... }X @Override void failure(Throwable t) { // ... }X }); -
Response Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); call.enqueue(new Callback<List<Contributor>>() { @Override
void onResponse(Response<List<Contributor>> response) { // ... } @Override void failure(Throwable t) { // ... } }); -
Response class Response<T> { int code(); String message(); Headers headers();
boolean isSuccess(); }X -
Response class Response<T> { int code(); String message(); Headers headers();
boolean isSuccess(); T body(); ResponseBody errorBody(); }X -
Response class Response<T> { int code(); String message(); Headers headers();
boolean isSuccess(); T body(); ResponseBody errorBody(); com.squareup.okhttp.Response raw(); }X -
Fixed Query Param interface SomeService { @GET("/some/endpoint?fixed=query") Call<SomeResponse> someEndpoint(); }
someService.someEndpoint(); // GET /some/endpoint?fixed=query HTTP/1.1 -
Dynamic Query Param interface SomeService { @GET("/some/endpoint") Call<SomeResponse> someEndpoint( @Query("dynamic")
String dynamic); } someService.someEndpoint("query"); // GET /some/endpoint?dynamic=query HTTP/1.1 -
Dynamic Query Param Map interface SomeService { @GET("/some/endpoint") Call<SomeResponse> someEndpoint(
@QueryMap Map<String, String> dynamic); } someService.someEndpoint( Collections.singletonMap("dynamic", "query")); // GET /some/endpoint?dynamic=query HTTP/1.1 -
Omit Dynamic Query Param interface SomeService { @GET("/some/endpoint") Call<SomeResponse> someEndpoint(
@Query("dynamic") String dynamic); } someService.someEndpoint(null); // GET /some/endpoint HTTP/1.1 -
Fixed+Dynamic Query Params interface SomeService { @GET("/some/endpoint?fixed=query") Call<SomeResponse> someEndpoint( @Query("dynamic")
String dynamic); } someService.someEndpoint("query"); // GET /some/endpoint?fixed=query&dynamic=query HTTP/1.1 -
Path Replacement interface SomeService { @GET("/some/endpoint/{foo}") Call<SomeResponse> someEndpoint( @Path("thing") String
thing); } someService.someEndpoint("bar"); // GET /some/endpoint/bar HTTP/1.1 -
Fixed Header interface SomeService { @GET("/some/endpoint") @Headers("Accept-Encoding: application/json") Call<SomeResponse> someEndpoint();
} someService.someEndpoint(); // GET /some/endpoint HTTP/1.1 // Accept-Encoding: application/json -
Dynamic Header interface SomeService { @GET("/some/endpoint") Call<SomeResponse> someEndpoint( @Header("Location") String
location); } someService.someEndpoint("Droidcon NYC 2015"); // GET /some/endpoint HTTP/1.1 // Location: Droidcon NYC 2015 -
Omit Dynamic Header interface SomeService { @GET("/some/endpoint") Call<SomeResponse> someEndpoint( @Header("Location")
String location); } someService.someEndpoint(null); // GET /some/endpoint HTTP/1.1 -
Fixed+Dynamic Header interface SomeService { @GET("/some/endpoint") @Headers("Accept-Encoding: application/json") Call<SomeResponse> someEndpoint(
@Header("Location") String location); } someService.someEndpoint("Droidcon NYC 2015"); // GET /some/endpoint HTTP/1.1 // Accept-Encoding: application/json // Location: Droidcon NYC 2015 -
Post Without Body interface SomeService { @POST("/some/endpoint") Call<SomeResponse> someEndpoint(); }
someService.someEndpoint(); // POST /some/endpoint?fixed=query HTTP/1.1 // Content-Length: 0 -
Post With Body interface SomeService { @POST("/some/endpoint") Call<SomeResponse> someEndpoint( @Body
SomeRequest body); } someService.someEndpoint(); // POST /some/endpoint HTTP/1.1 // Content-Length: 3 // Content-Type: greeting // // Hi! -
Form Encoded Fields interface SomeService { @FormUrlEncoded @POST("/some/endpoint") Call<SomeResponse> someEndpoint(
@Field("name1") String name1, @Field("name2") String name2); } someService.someEndpoint("value1", "value2"); // POST /some/endpoint HTTP/1.1 // Content-Length: 25 // Content-Type: application/x-www-form-urlencoded // // name1=value1&name2=value2 -
Omit Form Encoded Field interface SomeService { @FormUrlEncoded @POST("/some/endpoint") Call<SomeResponse>
someEndpoint( @Field("name1") String name1, @Field("name2") String name2); } someService.someEndpoint("value1", null); // POST /some/endpoint HTTP/1.1 // Content-Length: 12 // Content-Type: application/x-www-form-urlencoded // // name1=value1 -
Form Encoded Field Map interface SomeService { @FormUrlEncoded @POST("/some/endpoint") Call<SomeResponse>
someEndpoint( @FieldMap Map<String, String> names); } someService.someEndpoint( ImmutableMap.of("name1", "value1", "name2", "value2")); // POST /some/endpoint HTTP/1.1 // Content-Length: 25 // Content-Type: application/x-www-form-urlencoded // // name1=value1&name2=value2 -
Multipart Parts interface SomeService { @Multipart @POST("/some/endpoint") Call<SomeResponse> someEndpoint( @Part("name1")
String name1, @Part("name2") String name2); } someService.someEndpoint("value1", "value2"); // POST /some/endpoint HTTP/1.1 // Content-Length: 102 // Content-Type: application/form-data // // ... -
Omit Multipart Part interface SomeService { @Multipart @POST("/some/endpoint") Call<SomeResponse> someEndpoint(
@Part("name1") String name1, @Part("name2") String name2); } someService.someEndpoint("value1", null); // POST /some/endpoint HTTP/1.1 // Content-Length: 56 // Content-Type: application/form-data // // ... -
Multipart Part Map interface SomeService { @Multipart @POST("/some/endpoint") Call<SomeResponse> someEndpoint(
@PartMap Map<String, String> names); } someService.someEndpoint( ImmutableMap.of("name1", "value1", "name2", "value2")); // POST /some/endpoint HTTP/1.1 // Content-Length: 102 // Content-Type: application/form-data // // ... -
Retrofit 2 • Call encapsulates single request/response interaction • Parameterized
Response object • Dynamic URL parameter -
Dynamic URL interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String
owner, @Path("repo") String repo); } -
Dynamic URL interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String
owner, @Path("repo") String repo); } Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); Response<List<Contributor>> response = call.execute(); -
Dynamic URL interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String
owner, @Path("repo") String repo); } Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); Response<List<Contributor>> response = call.execute(); // HTTP/1.1 200 OK // Link: <https://api.github.com/repositories/892275/contributors? page=2>; rel="next", <https://api.github.com/repositories/892275/ contributors?page=3>; rel="last" // ... -
Dynamic URL Response<List<Contributor>> response = call.execute(); // HTTP/1.1 200 OK
// Link: <https://api.github.com/repositories/892275/contributors? page=2>; rel="next", <https://api.github.com/repositories/892275/ contributors?page=3>; rel="last" // ... -
Dynamic URL Response<List<Contributor>> response = call.execute(); // HTTP/1.1 200 OK
// Link: <https://api.github.com/repositories/892275/contributors? page=2>; rel="next", <https://api.github.com/repositories/892275/ contributors?page=3>; rel="last" // ... String links = response.headers().get("Link"); -
Dynamic URL Response<List<Contributor>> response = call.execute(); // HTTP/1.1 200 OK
// Link: <https://api.github.com/repositories/892275/contributors? page=2>; rel="next", <https://api.github.com/repositories/892275/ contributors?page=3>; rel="last" // ... String links = response.headers().get("Link"); String nextLink = nextFromGitHubLinks(links); -
Dynamic URL Response<List<Contributor>> response = call.execute(); // HTTP/1.1 200 OK
// Link: <https://api.github.com/repositories/892275/contributors? page=2>; rel="next", <https://api.github.com/repositories/892275/ contributors?page=3>; rel="last" // ... String links = response.headers().get("Link"); String nextLink = nextFromGitHubLinks(links); // https://api.github.com/repositories/892275/contributors?page=2 -
Dynamic URL interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String
owner, @Path("repo") String repo); }X -
Dynamic URL interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String
owner, @Path("repo") String repo); @GET Call<List<Contributor>> repoContributorsPaginate( @Url String url); }X -
Dynamic URL String nextLink = nextFromGitHubLinks(links); // https://api.github.com/repositories/892275/contributors?page=2 Call<List<Contributor>> nextCall
= gitHubService.repoContributorsPaginate(nextLink); -
Retrofit 2 • Call encapsulates single request/response interaction • Parameterized
Response object • Dynamic URL parameter • Multiple, efficient converters -
Converters interface SomeProtoService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); }X interface SomeJsonService
{ @GET("/some/json/endpoint") Call<SomeJsonResponse> someJsonEndpoint(); }X -
Converters interface SomeService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); @GET("/some/json/endpoint") Call<SomeJsonResponse>
someJsonEndpoint(); }X SomeProtoResponse -
Converters interface SomeService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); @GET("/some/json/endpoint") Call<SomeJsonResponse>
someJsonEndpoint(); }X SomeProtoResponse -
Converters interface SomeService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); @GET("/some/json/endpoint") Call<SomeJsonResponse>
someJsonEndpoint(); }X SomeProtoResponse Proto? Yes! -
Converters interface SomeService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); @GET("/some/json/endpoint") Call<SomeJsonResponse>
someJsonEndpoint(); }X SomeJsonResponse -
Converters interface SomeService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); @GET("/some/json/endpoint") Call<SomeJsonResponse>
someJsonEndpoint(); }X SomeJsonResponse Proto? No! -
Converters interface SomeService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); @GET("/some/json/endpoint") Call<SomeJsonResponse>
someJsonEndpoint(); }X SomeJsonResponse Proto? No! JSON? Yes! -
Retrofit 2 • Call encapsulates single request/response interaction • Parameterized
Response object • Dynamic URL parameter • Multiple, efficient converters • Multiple, pluggable execution mechanisms -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String
owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> repoContributors2( @Path("owner") String owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3( @Path("owner") String owner, @Path("repo") String repo); }X -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Call -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Call -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Call RxJava? No! -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Call RxJava? No! Call? Yes! -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Observable -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Observable RxJava? Yes! -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Future -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Future RxJava? No! -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Future RxJava? No! Call? No! -
Execution Mechanisms interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors2(..); @GET("/repos/{owner}/{repo}/contributors") Future<List<Contributor>> repoContributors3(..); }X Future RxJava? No! Call? No! Throw! -
Retrofit 2 • Call encapsulates single request/response interaction • Parameterized
Response object • Dynamic URL parameter • Multiple, efficient converters • Multiple, pluggable execution mechanisms -
Powered by OkHttp • In 2012 we needed client abstractions.
• In 2012 we needed request/response abstractions. -
Powered by OkHttp • In 2012 we needed client abstractions.
• In 2012 we needed request/response abstractions. • In 2012 we needed header abstractions. -
Powered by OkHttp • In 2012 we needed client abstractions.
• In 2012 we needed request/response abstractions. • In 2012 we needed header abstractions. • It's 2015. OkHttp is small, lean, focused, and full-featured. -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> RequestBody ResponseBody
Socket Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody
ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T> -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } GitHubService gitHubService = retrofit.create(GitHubService.class); -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } // https://api.github.com/repos/square/retrofit/contributors -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } // https://api.github.com/repos/square/retrofit/contributors HttpUrl -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } // https://api.github.com/repos/square/retrofit/contributors HttpUrl .resolve() -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } // https://api.github.com/repos/square/retrofit/contributors -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } // https://api.github.com/repos/square/retrofit/contributors HttpUrl .resolve() -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); }X -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); }X -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } -
Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/v3/") .build(); interface GitHubService
{ @GET("repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors( @Path("owner") String owner, @Path("repo") String repo); } // https://api.github.com/v3/repos/square/retrofit/contributors -
Setup OkHttpClient client = new OkHttpClient(); Retrofit retrofit = new
Retrofit.Builder() .baseUrl("https://api.github.com") .client(client) .build(); -
Setup OkHttpClient client = new OkHttpClient(); client.interceptors().add(..); Retrofit retrofit =
new Retrofit.Builder() .baseUrl("https://api.github.com") .client(client) .build(); -
Converter.Factory interface SomeService { @GET("/some/proto/endpoint") Call<SomeProtoResponse> someProtoEndpoint(); @GET("/some/json/endpoint") Call<SomeJsonResponse>
someJsonEndpoint(); }X SomeJsonResponse Proto? No! JSON? Yes! -
Converter.Factory interface Converter<T> { interface Factory { Converter<?> create(Type type);
} }X SomeJsonResponse Proto? No! JSON? Yes! -
JSON Converter.Factory class ProtoConverterFactory { Converter<?> create(Type type); }X SomeJsonResponse
null No! Converter<?> Yes! class GsonConverterFactory { Converter<?> create(Type type); }X -
Converter.Factory interface Converter<T> { interface Factory { Converter<?> create(Type type);
}X T fromBody(ResponseBody body); RequestBody toBody(T value); }X -
CallAdapter.Factory interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> repoContributors(..); @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>>
repoContributors2(..); }X Call RxJava? No! Call? Yes! -
CallAdapter.Factory interface CallAdapter<T> { interface Factory { CallAdapter<?> create(Type type);
} }X Call RxJava? No! Call? Yes! -
CallAdapter.Factory Call class RxJavaCallAdapterFactory { CallAdapter<?> create(Type type); }X null
No! JSON CallAdapter<?> Yes! class CallAdapterFactory { CallAdapter<?> create(Type type); }X -
CallAdapter.Factory interface CallAdapter<T> { interface Factory { CallAdapter<?> create(Type type);
}X Type responseType(); Object adapt(Call<T> value); }X -
Under Construction • Parameter handlers • Logging? • Finalizing mock
module • Documentation -
Under Construction • Parameter handlers • Logging? • Finalizing mock
module • Documentation • WebSockets! (in v2.1)