Simple HTTP with Retrofit 2 (Droidcon NYC 2015)

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

E68309f117985270285ade8082f4877d?s=128

Jake Wharton

August 27, 2015
Tweet

Transcript

  1. 2.
  2. 3.

    Retrofit 2 will be out by the end of this

    year " " — Jake Wharton
  3. 4.

    Retrofit 2 will be out by the end of this

    year " " Droidcon NYC 2014 — Jake Wharton
  4. 5.

    Retrofit 2 will be out by the end of this

    year " " Droidcon NYC 2015 — Jake Wharton
  5. 8.
  6. 9.

    Retrofit 1 • Made open source on October 13th, 2010

    • Originally a Bob Lee joint • Took over / stole stewardship mid-2012
  7. 10.

    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
  8. 11.

    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
  9. 12.
  10. 15.

    The Good • Interface service declarations • Method and parameter

    annotations customize request • Pluggable HTTP client and serialization
  11. 19.

    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());
  12. 21.

    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());
  13. 22.

    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());
  14. 23.

    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());
  15. 24.

    The Good • Interface service declarations • Method and parameter

    annotations customize request • Pluggable HTTP client and serialization • Synchronous, asynchronous, and RxJava execution
  16. 25.

    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");
  17. 26.

    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) {
 // ...
 }
 });
  18. 27.

    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) {
 // ...
 }
 });
  19. 28.

    The Good • Interface service declarations • Method and parameter

    annotations customize request • Pluggable HTTP client and serialization • Synchronous, asynchronous, and RxJava execution
  20. 32.
  21. 33.

    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
  22. 34.

    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
  23. 35.

    The Not-So-Good • Request/Response (and friends) model classes • No

    access to serialized body and response data • Rigid execution mechanisms
  24. 36.

    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);
 }
  25. 40.

    The Not-So-Good • Request/Response (and friends) model classes • No

    access to serialized body and response data • Rigid execution mechanisms • Inefficient converter usage
  26. 42.

    Inefficient converter usage interface Converter {
 Object fromBody(TypedInput body, Type

    type);
 TypedOutput toBody(Object object);
 } Type TypeAdapter TypedInput/Output JsonAdapter ObjectReader/Writer Parser ProtoAdapter
  27. 43.

    Inefficient converter usage interface Converter {
 Object fromBody(TypedInput body, Type

    type);
 TypedOutput toBody(Object object);
 } Type TypeAdapter TypedInput/Output JsonAdapter ObjectReader/Writer Parser ProtoAdapter
  28. 44.

    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
  29. 46.

    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
  30. 47.

    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
  31. 48.

    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
  32. 53.

    Call • Models a single request/response pair • Separates request

    creation from response handling • Each instance can only be used once...
  33. 54.

    Call • Models a single request/response pair • Separates request

    creation from response handling • Each instance can only be used once... • ...instances can be cloned
  34. 55.

    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.
  35. 56.

    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
  36. 60.

    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");
  37. 64.

    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();
  38. 66.

    Call Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); 
 call.enqueue(new Callback<List<Contributor>>() {


    @Override void onResponse(/* ... */) {
 // ...
 }
 
 @Override void onFailure(Throwable t) {
 // ...
 }
 });
  39. 73.

    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
 });
  40. 74.

    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
 });
  41. 75.

    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) {
 // ...
 }
 });
  42. 79.

    Response class Response<T> {
 int code();
 String message(); Headers headers();


    
 boolean isSuccess(); T body();
 ResponseBody errorBody();
 }X
  43. 80.

    Response class Response<T> {
 int code();
 String message(); Headers headers();


    
 boolean isSuccess(); T body();
 ResponseBody errorBody(); com.squareup.okhttp.Response raw();
 }X
  44. 84.

    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
  45. 85.

    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
  46. 86.

    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
  47. 87.

    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
  48. 89.

    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
  49. 90.

    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
  50. 91.

    Omit Dynamic Header interface SomeService {
 @GET("/some/endpoint")
 Call<SomeResponse> someEndpoint( @Header("Location")

    String location);
 } someService.someEndpoint(null); // GET /some/endpoint HTTP/1.1
  51. 92.

    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
  52. 93.

    Post Without Body interface SomeService {
 @POST("/some/endpoint")
 Call<SomeResponse> someEndpoint();
 }

    someService.someEndpoint(); // POST /some/endpoint?fixed=query HTTP/1.1 // Content-Length: 0
  53. 94.

    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!
  54. 95.

    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
  55. 96.

    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
  56. 97.

    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
  57. 98.

    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 // // ...
  58. 99.

    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 // // ...
  59. 100.

    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 // // ...
  60. 104.

    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();
  61. 105.

    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" // ...
  62. 106.

    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" // ...
  63. 107.

    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");
  64. 108.

    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);
  65. 109.

    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
  66. 111.

    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
  67. 114.

    Retrofit 2 • Call encapsulates single request/response interaction • Parameterized

    Response object • Dynamic URL parameter • Multiple, efficient converters
  68. 122.

    Retrofit 2 • Call encapsulates single request/response interaction • Parameterized

    Response object • Dynamic URL parameter • Multiple, efficient converters • Multiple, pluggable execution mechanisms
  69. 124.

    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
  70. 125.

    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
  71. 126.

    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
  72. 127.

    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!
  73. 128.

    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!
  74. 129.

    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
  75. 130.

    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!
  76. 131.

    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
  77. 132.

    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!
  78. 133.

    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!
  79. 134.

    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!
  80. 135.

    Retrofit 2 • Call encapsulates single request/response interaction • Parameterized

    Response object • Dynamic URL parameter • Multiple, efficient converters • Multiple, pluggable execution mechanisms
  81. 138.

    Powered by OkHttp • In 2012 we needed client abstractions.

    • In 2012 we needed request/response abstractions.
  82. 139.

    Powered by OkHttp • In 2012 we needed client abstractions.

    • In 2012 we needed request/response abstractions. • In 2012 we needed header abstractions.
  83. 140.

    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.
  84. 143.
  85. 153.
  86. 154.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  87. 155.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  88. 156.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  89. 157.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  90. 158.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  91. 159.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  92. 160.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  93. 161.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  94. 162.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  95. 163.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  96. 164.

    Retrofit OkHttp MoshiConverter BufferedSource BufferedSink RequestBody ResponseBody <T> MyService RequestBody

    ResponseBody Socket execute() myEndpoint() Moshi BufferedSink BufferedSource <T>
  97. 166.

    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);
 }
  98. 167.

    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);
  99. 168.

    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
  100. 169.

    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
  101. 170.

    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()
  102. 171.

    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);
 }
  103. 172.

    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);
 }
  104. 173.

    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);
 }
  105. 174.

    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
  106. 175.

    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()
  107. 176.

    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
  108. 177.

    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
  109. 178.

    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);
 }
  110. 179.

    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
  111. 181.

    Setup OkHttpClient client = new OkHttpClient(); Retrofit retrofit = new

    Retrofit.Builder() .baseUrl("https://api.github.com") .client(client) .build();
  112. 182.

    Setup OkHttpClient client = new OkHttpClient(); client.interceptors().add(..); Retrofit retrofit =

    new Retrofit.Builder() .baseUrl("https://api.github.com") .client(client) .build();
  113. 193.

    JSON Converter.Factory class ProtoConverterFactory { Converter<?> create(Type type); }X SomeJsonResponse

    null No! Converter<?> Yes! class GsonConverterFactory { Converter<?> create(Type type); }X
  114. 195.

    Converter.Factory interface Converter<T> { interface Factory { Converter<?> create(Type type);

    }X T fromBody(ResponseBody body); RequestBody toBody(T value); }X
  115. 201.

    CallAdapter.Factory Call class RxJavaCallAdapterFactory { CallAdapter<?> create(Type type); }X null

    No! JSON CallAdapter<?> Yes! class CallAdapterFactory { CallAdapter<?> create(Type type); }X
  116. 210.

    Under Construction • Parameter handlers • Logging? • Finalizing mock

    module • Documentation • WebSockets! (in v2.1)
  117. 211.