Making Retrofit Work For You (Ohio DevFest 2016)

Making Retrofit Work For You (Ohio DevFest 2016)

Retrofit's recently-released version 2 is the easiest way to do HTTP in your applications. Once set up, Retrofit is designed to make declaring endpoints as simple as a method on an interface with annotations. Behind that simplicity, though, there is a lot of power and knowing how to use and configure it can make even the most problematic APIs easy to use.

This talk will focus on configuring three parts of Retrofit:

* Endpoint declarations on interfaces which define how requests are created and sent and how their responses are read and parsed.

* Serialization customization that goes beyond just choosing between JSON or Protocol Buffers.

* Execution mechanisms which enable synchronous or asynchronous requests, integration with third-party libraries, and custom error handling.

You will not need prior Retrofit experience, although the focus will specifically be on configuring and customizing its behavior.

E68309f117985270285ade8082f4877d?s=128

Jake Wharton

November 19, 2016
Tweet

Transcript

  1. 4.

    Retrofit interface ApiService { List<User> search( String query, SortOrder order);

    }A class HttpApiService implements ApiService { // ... }
  2. 11.

    Retrofit interface ApiService { @GET("/search/{category}") Call<List<User>> search( @Path("category") String category,

    @Query("q") String query, @Query("sort") SortOrder order); @POST("/upload/image") Call<Void> uploadImage( @Body Image image); }A
  3. 12.

    Retrofit interface ApiService { @GET("/search/{category}") Call<List<User>> search( @Path("category") String category,

    @Query("q") String query, @Query("sort") SortOrder order); @POST("/upload/image") @Headers("SomeHeader: SomeValue") Call<Void> uploadImage( @Body Image image); }A
  4. 14.

    Retrofit interface ApiService { // ... }A Retrofit retrofit =

    new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build();
  5. 15.

    Retrofit interface ApiService { // ... }A Retrofit retrofit =

    new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build(); ApiService service = retrofit.create(ApiService.class);
  6. 16.

    Retrofit interface ApiService { // ... }A Retrofit retrofit =

    new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build(); ApiService service = retrofit.create(ApiService.class);
  7. 19.

    HTTP Client Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build(); Foo

    foo = retrofit.create(Foo.class); Bar bar = retrofit.create(Bar.class);
  8. 20.

    HTTP Client Retrofit retrofitFoo = new Retrofit.Builder()
 .baseUrl("http://foo.example.com")
 .build(); Foo

    foo = retrofitFoo.create(Foo.class); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .build(); Bar bar = retrofitBar.create(Bar.class);
  9. 21.

    HTTP Client Retrofit retrofitFoo = new Retrofit.Builder()
 .baseUrl("http://foo.example.com") .client(new OkHttpClient())


    .build(); Foo foo = retrofitFoo.create(Foo.class); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com") .client(new OkHttpClient())
 .build(); Bar bar = retrofitBar.create(Bar.class);
  10. 22.

    HTTP Client Retrofit retrofitFoo = new Retrofit.Builder()
 .baseUrl("http://foo.example.com") .client(new OkHttpClient())


    .build(); Foo foo = retrofitFoo.create(Foo.class); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com") .client(new OkHttpClient())
 .build(); Bar bar = retrofitBar.create(Bar.class); OkHttpClient client =
  11. 23.

    HTTP Client OkHttpClient client = new OkHttpClient(); Retrofit retrofitFoo =

    new Retrofit.Builder()
 .baseUrl("http://foo.example.com") .client(client)
 .build(); Foo foo = retrofitFoo.create(Foo.class); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com") .client(client)
 .build(); Bar bar = retrofitBar.create(Bar.class); new OkHttpClient()
  12. 24.

    HTTP Client OkHttpClient client = new OkHttpClient(); Retrofit retrofitFoo =

    new Retrofit.Builder()
 .baseUrl("http://foo.example.com") .client(client)
 .build(); Foo foo = retrofitFoo.create(Foo.class); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com") .client(client)
 .build(); Bar bar = retrofitBar.create(Bar.class);
  13. 26.

    HTTP Client OkHttpClient client = new OkHttpClient(); OkHttpClient clientFoo =

    client.newBuilder() .addInterceptor(new FooInterceptor()) .build();
  14. 27.

    HTTP Client OkHttpClient client = new OkHttpClient(); OkHttpClient clientFoo =

    client.newBuilder() .addInterceptor(new FooInterceptor()) .build(); OkHttpClient clientBar = client.newBuilder() .readTimeout(30, SECONDS) .writeTimeout(30, SECONDS) .build();
  15. 28.

    HTTP Client interface Service {
 @GET("/user")
 Call<User> user();
 
 @POST("/login")


    Call<User> login(@Body LoginRequest request);
 @GET("/logout")
 Call<Void> logout();
 }X
  16. 29.

    HTTP Client interface Service {
 @GET("/user") // Requires authentication.
 Call<User>

    user();
 
 @POST("/login") // No authentication.
 Call<User> login(@Body LoginRequest request);
 @GET("/logout") // Requires authentication.
 Call<Void> logout();
 }X
  17. 30.

    HTTP Client class ServiceInterceptor implements Interceptor {
 @Override public Response

    intercept(Chain chain) {
 Request request = chain.request();
 return chain.proceed(request);
 }B
 }A
  18. 31.

    HTTP Client class ServiceInterceptor implements Interceptor {
 @Override public Response

    intercept(Chain chain) {
 Request request = chain.request(); 
 if (!request.url().encodedPath().equals("/login")) {
 request = request.newBuilder()
 .addHeader("Authorization", "hunter2")
 .build();
 } 
 return chain.proceed(request);
 }B
 }A
  19. 32.

    HTTP Client class ServiceInterceptor implements Interceptor {
 @Override public Response

    intercept(Chain chain) {
 Request request = chain.request(); 
 if (!request.url().encodedPath().equals("/login")) {
 request = request.newBuilder()
 .addHeader("Authorization", "hunter2")
 .build();
 } 
 return chain.proceed(request);
 }
 }
  20. 33.

    HTTP Client interface Service {
 @GET("/user") // Requires authentication.
 Call<User>

    user();
 
 @POST("/login") // No authentication.
 Call<User> login(@Body LoginRequest request);
 @GET("/logout") // Requires authentication.
 Call<Void> logout();
 }X
  21. 34.

    HTTP Client interface Service {
 @GET("/user") // Requires authentication.
 Call<User>

    user();
 
 @POST("/login") // No authentication.
 Call<User> login(@Body LoginRequest request);
 
 @POST("/forgotPassword") // No authentication.
 Call<Void> forgotPassword( @Body ForgotPasswordRequest request);
 @GET("/logout") // Requires authentication.
 Call<Void> logout();
 }X
  22. 35.

    HTTP Client interface Service {
 @GET("/user") // Requires authentication.
 Call<User>

    user();
 
 @POST("/login") // No authentication.
 Call<User> login(@Body LoginRequest request);
 
 @POST("/forgotPassword") // No authentication.
 Call<Void> forgotPassword( @Body ForgotPasswordRequest request);
 @GET("/logout") // Requires authentication.
 Call<Void> logout();
 }X class ServiceInterceptor implements Interceptor {
 @Override public Response intercept(Chain chain) {
 Request request = chain.request(); 
 if (!request.url().encodedPath().equals("/login")) {
 request = request.newBuilder()
 .addHeader("Authorization", "hunter2")
 .build();
 } 
 return chain.proceed(request);
 }
 }
  23. 36.

    HTTP Client interface Service {
 @GET("/user") // Requires authentication.
 Call<User>

    user();
 
 @POST("/login") // No authentication.
 Call<User> login(@Body LoginRequest request);
 
 @POST("/forgotPassword") // No authentication.
 Call<Void> forgotPassword( @Body ForgotPasswordRequest request);
 @GET("/logout") // Requires authentication.
 Call<Void> logout();
 }X
  24. 37.

    HTTP Client interface Service {
 @GET("/user")
 Call<User> user();
 
 @POST("/login")

    // No authentication.
 Call<User> login(@Body LoginRequest request);
 
 @POST("/forgotPassword") // No authentication.
 Call<Void> forgotPassword( @Body ForgotPasswordRequest request);
 @GET("/logout")
 Call<Void> logout();
 }X
  25. 38.

    HTTP Client interface Service {
 @GET("/user")
 Call<User> user();
 
 @POST("/login")

    @Headers("No-Authentication: true")
 Call<User> login(@Body LoginRequest request);
 
 @POST("/forgotPassword") @Headers("No-Authentication: true")
 Call<Void> forgotPassword( @Body ForgotPasswordRequest request);
 @GET("/logout")
 Call<Void> logout();
 }X 
 
 
 
 // No authentication.
 
 
 // No authentication.
  26. 39.

    HTTP Client interface Service {
 @GET("/user")
 Call<User> user();
 
 @POST("/login")

    @Headers("No-Authentication: true")
 Call<User> login(@Body LoginRequest request);
 
 @POST("/forgotPassword") @Headers("No-Authentication: true")
 Call<Void> forgotPassword( @Body ForgotPasswordRequest request);
 @GET("/logout")
 Call<Void> logout();
 }X
  27. 40.

    HTTP Client class ServiceInterceptor implements Interceptor {
 @Override public Response

    intercept(Chain chain) {
 Request request = chain.request(); 
 if (!request.url().encodedPath().equals("/login")) {
 request = request.newBuilder()
 .addHeader("Authorization", "hunter2")
 .build();
 }C 
 return chain.proceed(request);
 }B
 }A
  28. 41.

    HTTP Client class ServiceInterceptor implements Interceptor {
 @Override public Response

    intercept(Chain chain) {
 Request request = chain.request(); 
 if (request.header("No-Authentication") == null) {
 request = request.newBuilder()
 .addHeader("Authorization", "hunter2")
 .build();
 }C 
 return chain.proceed(request);
 }B
 }A 
 
 
 ! url().encodedPath().equals("/login")
  29. 42.

    HTTP Client class ServiceInterceptor implements Interceptor {
 @Override public Response

    intercept(Chain chain) {
 Request request = chain.request(); 
 if (request.header("No-Authentication") == null) {
 request = request.newBuilder()
 .addHeader("Authorization", "hunter2")
 .build();
 }C 
 return chain.proceed(request);
 }B
 }A
  30. 48.
  31. 51.

    Converters Retrofit retrofitFoo = new Retrofit.Builder()
 .baseUrl("http://foo.example.com")
 .addConverterFactory( GsonConverterFactory.create())
 .build();

    Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .addConverterFactory( GsonConverterFactory.create())
 .build();
  32. 52.

    Converters Retrofit retrofitFoo = new Retrofit.Builder()
 .baseUrl("http://foo.example.com")
 .addConverterFactory( GsonConverterFactory.create())
 .build();

    Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .addConverterFactory( GsonConverterFactory.create())
 .build();
  33. 53.

    Converters Retrofit retrofitFoo = new Retrofit.Builder()
 .baseUrl("http://foo.example.com")
 .addConverterFactory( GsonConverterFactory.create())A
 .build();

    Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .addConverterFactory( GsonConverterFactory.create())B
 .build();
  34. 54.

    Converters GsonConverterFactory gsonFactory = GsonConverterFactory.create(); Retrofit retrofitFoo = new Retrofit.Builder()


    .baseUrl("http://foo.example.com")
 .addConverterFactory(gsonFactory)A
 .build(); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .addConverterFactory(gsonFactory)B
 .build(); GsonConverterFactory.create()
  35. 55.

    Converters GsonConverterFactory gsonFactory = GsonConverterFactory.create(); Retrofit retrofitFoo = new Retrofit.Builder()


    .baseUrl("http://foo.example.com")
 .addConverterFactory(gsonFactory)A
 .build(); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .addConverterFactory(gsonFactory)B
 .build();
  36. 56.

    Converters GsonConverterFactory gsonFactory = GsonConverterFactory.create(); Retrofit retrofitFoo = new Retrofit.Builder()


    .baseUrl("http://foo.example.com")
 .addConverterFactory(gsonFactory)
 .build(); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .addConverterFactory(gsonFactory)
 .build();
  37. 57.

    Converters Gson gson = new Gson(); GsonConverterFactory gsonFactory = GsonConverterFactory.create(gson);

    Retrofit retrofitFoo = new Retrofit.Builder()
 .baseUrl("http://foo.example.com")
 .addConverterFactory(gsonFactory)
 .build(); Retrofit retrofitBar = new Retrofit.Builder()
 .baseUrl("http://bar.example.com")
 .addConverterFactory(gsonFactory)
 .build();
  38. 59.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory( GsonConverterFactory.create())
 .build();

    interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  39. 60.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory( GsonConverterFactory.create())
 .build();

    interface Service {
 @GET("/user")
 Call<User> user(); // <-- proto 
 @GET("/friends")
 Call<Friends> friends(); // <-- json
 }X
  40. 61.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); // <-- proto 
 @GET("/friends")
 Call<Friends> friends(); // <-- json
 }X
  41. 62.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  42. 63.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  43. 64.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create()) User

    ?
 .addConverterFactory( GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  44. 65.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create()) User

    ✓
 .addConverterFactory( GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  45. 66.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  46. 67.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  47. 68.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  48. 69.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create()) Friends

    ?
 .addConverterFactory( GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  49. 70.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create()) Friends

    ✗
 .addConverterFactory( GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  50. 71.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create()) Friends

    ✗
 .addConverterFactory( GsonConverterFactory.create()) 
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  51. 72.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create()) Friends

    ✗
 .addConverterFactory( GsonConverterFactory.create()) Friends ?
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  52. 73.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create()) Friends

    ✗
 .addConverterFactory( GsonConverterFactory.create()) Friends ✓
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  53. 74.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  54. 75.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory( GsonConverterFactory.create()) .addConverterFactory(

    ProtoConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  55. 76.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory( GsonConverterFactory.create()) .addConverterFactory(

    ProtoConverterFactory.create()) // No!
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  56. 77.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( ProtoConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); 
 @GET("/friends")
 Call<Friends> friends();
 }X
  57. 78.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( SimpleXmlConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); // <-- xml 
 @GET("/friends")
 Call<Friends> friends(); // <-- json
 }X
  58. 79.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( SimpleXmlConverterFactory.create())
 .addConverterFactory(

    GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user(); // <-- xml 
 @GET("/friends")
 Call<Friends> friends(); // <-- json
 }X
  59. 80.

    Converters interface Service {
 @GET("/user")
 Call<User> user(); // <-- xml

    
 @GET("/friends")
 Call<Friends> friends(); // <-- json
 }X
  60. 81.

    Converters interface Service {
 @GET("/user")
 Call<User> user(); // <-- xml

    
 @GET("/friends")
 Call<Friends> friends(); // <-- json
 }X @interface Xml {}
  61. 82.

    Converters interface Service {
 @GET("/user")
 Call<User> user(); // <-- xml

    
 @GET("/friends")
 Call<Friends> friends(); // <-- json
 }X @interface Xml {} @interface Json {}
  62. 83.

    Converters interface Service {
 @GET("/user") @Xml
 Call<User> user(); // <--

    xml 
 @GET("/friends") @Json
 Call<Friends> friends(); // <-- json
 }X @interface Xml {} @interface Json {}
  63. 84.

    Converters interface Service {
 @GET("/user") @Xml
 Call<User> user(); // <--

    xml 
 @GET("/friends") @Json
 Call<Friends> friends(); // <-- json
 }X @interface Xml {} @interface Json {}
  64. 86.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( new XmlOrJsonConverterFactory())X


    .build(); 
 SimpleXmlConverterFactory.create() 
 .addConverterFactory( GsonConverterFactory.create())
  65. 89.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( new XmlOrJsonConverterFactory())X


    .build(); class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml = SimpleXmlConverterFactory.create(); }G
  66. 90.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( new XmlOrJsonConverterFactory())X


    .build(); class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml = SimpleXmlConverterFactory.create(); final Converter.Factory json = GsonConverterFactory.create(); }G
  67. 91.

    Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( new XmlOrJsonConverterFactory())X
 .build();

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml = // ... final Converter.Factory json = // ... }G
  68. 92.

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml =

    // ... final Converter.Factory json = // ... @Override public Converter<ResponseBody,?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { }V }G
  69. 93.

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml =

    // ... final Converter.Factory json = // ... @Override public Converter<ResponseBody,?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { }V }G
  70. 94.

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml =

    // ... final Converter.Factory json = // ... @Override public Converter<ResponseBody,?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { for (Annotation annotation : annotations) { }F }V }G
  71. 95.

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml =

    // ... final Converter.Factory json = // ... @Override public Converter<ResponseBody,?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { for (Annotation annotation : annotations) { if (annotation.getClass() == Xml.class) { }I }F }V }G
  72. 96.

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml =

    // ... final Converter.Factory json = // ... @Override public Converter<ResponseBody,?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { for (Annotation annotation : annotations) { if (annotation.getClass() == Xml.class) { return xml.responseBodyConverter( type, annotations, retrofit); }I }F }V }G
  73. 97.

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml =

    // ... final Converter.Factory json = // ... @Override public Converter<ResponseBody, ?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { for (Annotation annotation : annotations) { if (annotation.getClass() == Xml.class) { return xml.responseBodyConverter( type, annotations, retrofit); }I if (annotation.getClass() == Json.class) { return json.responseBodyConverter( type, annotations, retrofit); }7 }F }V }G
  74. 98.

    Converters class XmlOrJsonConverterFactory extends Converter.Factory { final Converter.Factory xml =

    // ... final Converter.Factory json = // ... @Override public Converter<ResponseBody, ?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { for (Annotation annotation : annotations) { if (annotation.getClass() == Xml.class) { return xml.responseBodyConverter( type, annotations, retrofit); }I if (annotation.getClass() == Json.class) { return json.responseBodyConverter( type, annotations, retrofit); }7 }F return null; }V }G
  75. 99.

    Converters class AnnotatedConverterFactory extends Converter.Factory { final Map<Class<?>, Converter.Factory> factories;

    AnnotationConverterFactory( Map<Class<?>, Converter.Factory> factories) { this.factories = new LinkedHashMap<>(factories); } @Override public Converter<ResponseBody,?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { for (Annotation annotation : annotations) { Converter.Factory factory = factories.get(annotation.getClass()); if (factory != null) { return factory.responseBodyConverter( type, annotations, retrofit); }I }F return null; }V }G
  76. 101.

    Converters class AnnotatedConverterFactory extends Converter.Factory { // ... class Builder

    { Map<Class<?>,Converter.Factory> factories = new LinkedHashMap<>(); Builder add(Class<? extends Annotation> cls, Converter.Factory factory) { if (cls == null) throw new NullPointerException("cls"); if (factory == null) throw new NullPointerException("factory"); factories.add(cls, factory); return this; } AnnotationConverterFactory build() { return new AnnotatedConverterFactory(factories); } }G
  77. 103.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory( AnnotatedConverterFactory.builder() .add(Xml.class,

    SimpleXmlConverterFactory.create()) .add(Json.class, GsonConverterFactory.create()) .build())X
 .build(); 
 new XmlOrJsonConverterFactory()
  78. 106.

    Converters {Y "meta": { "code": 200, "time": 287, }, "notifications":

    {}, "response": {X "id": "1345235987", "name": "Shrek", "location": "Swamp" }X }Y
  79. 107.

    Converters {Y "meta": { "code": 200, "time": 287, }, "notifications":

    {}, "response": {X "id": "1345235987", "name": "Shrek", "location": "Swamp" }X }Y
  80. 108.

    Converters {Y "meta": { "code": 200, "time": 287, }, "notifications":

    {}, "response": {X "id": "1345235987", "name": "Shrek", "location": "Swamp" }X }Y
  81. 109.

    Converters {Y "meta": { "code": 200, "time": 287, }, "notifications":

    {}, "response": {X "id": "1345235987", "name": "Shrek", "location": "Swamp" }X }Y
  82. 111.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory(GsonConverterFactory.create())
 .build(); interface

    Service {
 @GET("/user")
 Call<User> user();
 }X class Envelope<T> { Meta meta; List<Notification> notifications; T response; }Z
  83. 112.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory(GsonConverterFactory.create())
 .build(); interface

    Service {
 @GET("/user")
 Call<Envelope<User>> user();
 }X class Envelope<T> { Meta meta; List<Notification> notifications; T response; }Z
  84. 113.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory(GsonConverterFactory.create())
 .build(); interface

    Service {
 @GET("/user")
 Call<Envelope<User>> user();
 }X class Envelope<T> { Meta meta; List<Notification> notifications; T response; }Z
  85. 118.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody,?> responseBodyConverter(

    Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); }B }A // Envelope.class parameterized with type
  86. 119.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody,?> responseBodyConverter(

    Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); }B }A
  87. 120.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody,?> responseBodyConverter(

    Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); return new Converter<ResponseBody, ?>() { }; }B }A
  88. 121.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { }B }; }B }A
  89. 122.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); final Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { Envelope<?> envelope = delegate.convert(body); }B }; }B }A
  90. 123.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); final Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { Envelope<?> envelope = delegate.convert(body); return envelope.response; }B }; }B }A
  91. 124.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); final Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { Envelope<?> envelope = delegate.convert(body); return envelope.response; }B }; }B }A
  92. 125.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .addConverterFactory(GsonConverterFactory.create())
 .build(); interface

    Service {
 @GET("/user")
 Call<Envelope<User>> user();
 }X class Envelope<T> { Meta meta; List<Notification> notifications; T response; }Z
  93. 126.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory(new EnvelopingConverter())
 .addConverterFactory(GsonConverterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Call<Envelope<User>> user();
 }X class Envelope<T> { Meta meta; List<Notification> notifications; T response; }Z
  94. 127.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory(new EnvelopingConverter())
 .addConverterFactory(GsonConverterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Call<User> user();
 }X class Envelope<T> { Meta meta; List<Notification> notifications; T response; }Z
  95. 128.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory(new EnvelopingConverter())
 .addConverterFactory(GsonConverterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Call<User> user();
 }X class Envelope<T> { Meta meta; List<Notification> notifications; T response; }Z
  96. 129.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); final Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { Envelope<?> envelope = delegate.convert(body); return envelope.response; }B }; }B }A
  97. 130.

    Converters class EnvelopingConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Type envelopedType = TypeToken.getParameterized(Envelope.class, type) .getType(); final Converter<ResponseBody, Envelope<?>> delegate = retrofit.nextResponseBodyConverter( this, envelopedType, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { Envelope<?> envelope = delegate.convert(body); // Handle notifications, record timing information. return envelope.response; }B }; }B }A
  98. 131.

    Converters class EmptyToNullConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { }B }A
  99. 132.

    Converters class EmptyToNullConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter( this, type, annotations); }B }A
  100. 133.

    Converters class EmptyToNullConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter( this, type, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { }G }; }B }A
  101. 134.

    Converters class EmptyToNullConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter( this, type, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { if (body.contentLength() == 0) return null; }G }; }B }A
  102. 135.

    Converters class EmptyToNullConverter extends Converter.Factory { @Override public Converter<ResponseBody, ?>

    responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { final Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter( this, type, annotations); return new Converter<ResponseBody, ?>() { @Override public Object convert(ResponseBody body) { if (body.contentLength() == 0) return null; return delegate.convert(body); }G }; }B }A
  103. 137.

    Converters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addConverterFactory(new EmptyToNullConverter()) .addConverterFactory(new

    EnvelopingConverter())
 .addConverterFactory(GsonConverterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Call<User> user();
 }X
  104. 138.
  105. 139.

    Converters interface DribbbleSearchService { @GET("search") Call<List<Shot>> search( @Query("q") String query,

    @Query("page") Integer page, @Query("per_page") Integer pageSize, @Query("s") @SortOrder String sort); }
  106. 140.

    Converters interface DribbbleSearchService { @GET("search") Call<List<Shot>> search( @Query("q") String query,

    @Query("page") Integer page, @Query("per_page") Integer pageSize, @Query("s") @SortOrder String sort); }
  107. 141.

    Converters /** * Dribbble API does not have a search

    endpoint * so we have to do gross things :( */ class DribbbleSearchConverter { // ... }
  108. 142.

    Converters /** * Dribbble API does not have a search

    endpoint * so we have to do gross things :( */ class DribbbleSearchConverter { // ... }
  109. 143.

    Converters /** * Dribbble API does not have a search

    endpoint * so we have to do gross things :( */ class DribbbleSearchConverter { // ... } http://jakes.link/plaid-converter
  110. 154.

    Call Adapters interface Service {
 @GET("/user")
 Call<User> user();
 }X Call<User>

    Call<User> Call Call.Factory (aka OkHttpClient) CallAdapter
  111. 155.

    Call Adapters interface Service {
 @GET("/user")
 Call<User> user();
 }X Call<User>

    Call<User> Call Call.Factory (aka OkHttpClient) CallAdapter
  112. 156.

    Call Adapters interface Service {
 @GET("/user")
 Call<User> user();
 }X Call<User>

    Call<User> Call Call.Factory (aka OkHttpClient) CallAdapter
  113. 159.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Observable<User> user();
 }X 
 
 
 
 Call
  114. 160.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Observable<User> user();
 }X Observable<User> Call<User> Call Call.Factory (aka OkHttpClient) CallAdapter
  115. 161.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Observable<User> user();
 }X Observable<User> Call<User> Call Call.Factory (aka OkHttpClient) CallAdapter
  116. 162.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Observable<User> user();
 }X Observable<User> Call<User> Call Call.Factory (aka OkHttpClient) CallAdapter
  117. 163.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  118. 164.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  119. 165.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  120. 166.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  121. 167.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    ? .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  122. 168.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    ✓ .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  123. 169.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  124. 170.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  125. 171.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  126. 172.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    ? .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  127. 173.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    ✗ .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  128. 174.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    ✗ .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  129. 175.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    ✗ .addCallAdapterFactory( new BuiltInCallFactory()) // implict! ?
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  130. 176.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    ✗ .addCallAdapterFactory( new BuiltInCallFactory()) // implict! ✓
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  131. 177.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( new BuiltInCallFactory()) // implict!
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  132. 178.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())


    .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  133. 179.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( Java8CallAdapterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends();
 }X
  134. 180.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create())

    .addCallAdapterFactory( Java8CallAdapterFactory.create())
 .build(); interface Service {
 @GET("/user")
 Observable<User> user(); @GET("/friends") Call<List<User>> friends(); @GET("/enemies") CompletableFuture<List<User>> enemies();
 }X
  135. 183.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { }B }A
  136. 184.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Observable.class) { return null; }C }B }A
  137. 185.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Observable.class) { return null; }C CallAdapter<Object, Observable<?>> delegate = retrofit.nextCallAdapter(this, returnType, annotations); }B }A
  138. 186.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Observable.class) { return null; }C CallAdapter<Object, Observable<?>> delegate = retrofit.nextCallAdapter(this, returnType, annotations); return new CallAdapter<Observable<?>>() { @Override public Observable<?> adapt(Call<Object> call) { }E }D }B }A
  139. 187.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Observable.class) { return null; }C CallAdapter<Object, Observable<?>> delegate = retrofit.nextCallAdapter(this, returnType, annotations); return new CallAdapter<Observable<?>>() { @Override public Observable<?> adapt(Call<Object> call) { Observable<?> o = delegate.adapt(call); }E }D }B }A
  140. 188.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Observable.class) { return null; }C CallAdapter<Object, Observable<?>> delegate = retrofit.nextCallAdapter(this, returnType, annotations); return new CallAdapter<Observable<?>>() { @Override public Observable<?> adapt(Call<Object> call) { Observable<?> o = delegate.adapt(call); return o.observeOn(mainThread()); }E }D }B }A
  141. 189.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Observable.class) { return null; }C CallAdapter<Object, Observable<?>> delegate = retrofit.nextCallAdapter(this, returnType, annotations); return new CallAdapter<Observable<?>>() { @Override public Observable<?> adapt(Call<Object> call) { Observable<?> o = delegate.adapt(call); return o.observeOn(mainThread()); }E @Override public Type responseType() { return delegate.responseType(); } }D }B }A
  142. 190.

    Call Adapters class RxObserveOnCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?,

    ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Observable.class) { return null; }C CallAdapter<Object, Observable<?>> delegate = retrofit.nextCallAdapter(this, returnType, annotations); return new CallAdapter<Observable<?>>() { @Override public Observable<?> adapt(Call<Object> call) { Observable<?> o = delegate.adapt(call); return o.observeOn(mainThread()); }E @Override public Type responseType() { return delegate.responseType(); } }D }B }A
  143. 192.

    Call Adapters Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com") .addCallAdapterFactory( new

    RxObserveOnCallAdapterFactory()) .addCallAdapterFactory( RxJavaCallAdapterFactory.createWithScheduler(io()))
 .build(); interface Service {
 @GET("/user")
 Observable<User> user();
 }X
  144. 196.

    Call Adapters interface Service {
 @GET("/user")
 Call<User> user();
 }X Response<User>

    response = call.execute(); if (!response.isSuccessful()) { // handle error body? }
  145. 197.

    Call Adapters interface Service {
 @GET("/user")
 Call<User> user();
 }X Response<User>

    response = call.execute(); if (!response.isSuccessful()) { ResponseBody errorBody = response.errorBody(); }
  146. 198.

    Call Adapters interface Call<R, E> { Response<R, E> execute(); void

    enqueue(Callback<R, E> cb); } interface Callback<R, E> { void onResponse(Response<R, E> response); void onFailure(IOException e); } interface Response<R, E> { R body(); E error(); }
  147. 199.

    Call Adapters interface Call<R, E> { Response<R, E> execute(); void

    enqueue(Callback<R, E> cb); } interface Callback<R, E> { void onResponse(Response<R, E> response); void onFailure(IOException e); } interface Response<R, E> { R body(); E error(); }
  148. 200.

    Call Adapters interface Call<R, E> { Response<R, E> execute(); void

    enqueue(Callback<R, E> cb); }1 interface Callback<R, E> { void onResponse(Response<R, E> response);A void onFailure(IOException e); }2 interface Response<R, E> { R body(); E error(); }3
  149. 201.

    Call Adapters interface Call<R, E> { Response<R, E> execute(); void

    enqueue(Callback<R, E> cb); }1 interface Callback<R, E> { void onSuccess(R body);A void onError(E errorBody); void onFailure(IOException e); }2 Response Response< , > response interface Response<R, E> { R body(); E error(); }3
  150. 202.

    Call Adapters interface Call<R, E> { Response<R, E> execute(); void

    enqueue(Callback<R, E> cb); }1 interface Callback<R, E> { void onSuccess(R body);A void onClientError(E errorBody); void onServerError(String message); void onUnauthenticated(); void onFailure(IOException e); }2
  151. 203.
  152. 206.

    Mock Mode interface Service {
 @GET("/user")
 Call<User> user();
 }X Service

    service = retrofit.create(Service.class); class MockService implements Service { @Override public Call<List<User>> user() { return Calls.success(Arrays.asList( new User("Alice"), new User("Bob"))); } }
  153. 207.

    Mock Mode interface Service {
 @GET("/user")
 Call<User> user();
 }X Service

    service = retrofit.create(Service.class); class MockService implements Service { @Override public Call<List<User>> user() { return Calls.success(Arrays.asList( new User("Alice"), new User("Bob"))); }B }A Service fakeService = new MockService();
  154. 208.

    Mock Mode class MockService implements Service { @Override public Call<List<User>>

    user() { return Calls.success(Arrays.asList( new User("Alice"), new User("Bob"))); }B }A
  155. 209.

    Mock Mode class MockService implements Service { final BehaviorDelegate<Service> delegate;

    MockService(BehaviorDelegate<Service> delegate) { this.delegate = delegate; }C @Override public Call<List<User>> user() { return Calls.success(Arrays.asList( new User("Alice"), new User("Bob")));Z }B }A
  156. 210.

    Calls.success( ) Mock Mode class MockService implements Service { final

    BehaviorDelegate<Service> delegate; MockService(BehaviorDelegate<Service> delegate) { this.delegate = delegate; }C @Override public Call<List<User>> user() { List<User> response = Arrays.asList( new User("Alice"), new User("Bob"));Z return delegate .returningResponse(response).user(); }B }A
  157. 213.

    Mock Mode Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build(); NetworkBehavior

    behavior = NetworkBehavior.create(); MockRetrofit mock = new MockRetrofit.Builder() .networkBehavior(behavior) .build();
  158. 214.

    Mock Mode Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build(); NetworkBehavior

    behavior = NetworkBehavior.create(); MockRetrofit mock = new MockRetrofit.Builder() .networkBehavior(behavior) .build(); BehaviorDelegate<Service> delegate = mock.create(Service.class);
  159. 215.

    Mock Mode Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build(); NetworkBehavior

    behavior = NetworkBehavior.create(); MockRetrofit mock = new MockRetrofit.Builder() .networkBehavior(behavior) .build(); BehaviorDelegate<Service> delegate = mock.create(Service.class); Service mockService = new MockService(delegate);
  160. 216.

    Mock Mode Retrofit retrofit = new Retrofit.Builder()
 .baseUrl("http://example.com")
 .build(); NetworkBehavior

    behavior = NetworkBehavior.create(); MockRetrofit mock = new MockRetrofit.Builder() .networkBehavior(behavior) .build(); BehaviorDelegate<Service> delegate = mock.create(Service.class); Service mockService = new MockService(delegate);
  161. 221.
  162. 222.
  163. 223.
  164. 225.

    Retrofit • HTTP client sends the bits across the wire.

    • Converters manipulate request/response data.
  165. 226.

    Retrofit • HTTP client sends the bits across the wire.

    • Converters manipulate request/response data. • Call adapters change execution mechanism.
  166. 227.

    Retrofit • HTTP client sends the bits across the wire.

    • Converters manipulate request/response data. • Call adapters change execution mechanism. • Mock mode creates deterministic, fake data for testing.