Slide 1

Slide 1 text

Retrofit Two Recap @punchdruneker

Slide 2

Slide 2 text

• engineer at mixi, Inc • sidelines • DroidKaigi • teaching at schoo • Shibuya.apk • and so on… @punchdrunker

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Retrofit 2 beta released!

Slide 5

Slide 5 text

Retrofit 2 way public interface GitHub { @GET("/repos/{owner}/{repo}/contributors") Call> contributors( @Path("owner") Owner owner, @Path("repo") String repo); @GET Call> contributorsPaginate( @Url String url); } Parameter Type Dynamic Url

Slide 6

Slide 6 text

Setup Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); GitHub github = retrofit.create(GitHub.class); Owner square = new Owner(“square"); Call> call = github.contributors(square, "retrofit"); Multiple Converter Call encapsulates single request/response interaction

Slide 7

Slide 7 text

Asynchronous execution call.enqueue(new Callback>() { @Override public void onResponse(Response> response) { // Log.d(response.headers().toString()); // Log.d(response.raw()); // => okhttp/Response List contributors = response.body(); for (Contributor contributor : contributors) { Log.d(contributor.login + " (" + contributor.contributions + ")"); } } ….

Slide 8

Slide 8 text

Asynchronous execution call.enqueue(new Callback>() { @Override public void onResponse(Response> response) { // Log.d(response.headers().toString()); // Log.d(response.raw()); // => okhttp/Response List contributors = response.body(); for (Contributor contributor : contributors) { Log.d(contributor.login + " (" + contributor.contributions + ")"); } } …. Parameterized Response Object

Slide 9

Slide 9 text

Call (Class) • 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

Slide 10

Slide 10 text

Multiple Converter // We can user multiple converters public interface AwesomeService { @GET("/awesome/proto/endpoint") Call awesomeProtoEndpoint(); @GET("/awesome/json/endpoint") Call awesomeJsonEndpoint(); } ————————————————————————————————————————————- Retrofit retrofitMulti = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(ProtoConverterFactory.create()) .build();

Slide 11

Slide 11 text

Multiple execution mechanism public interface GreatService { @GET("/repos/{owner}/{repo}/contributors") Call> repoContributors( @Path("owner") String owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") Observable> repoContributors2( @Path("owner") String owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") Future> repoContributors3( @Path("owner") String owner, @Path("repo") String repo); }

Slide 12

Slide 12 text

Multiple execution mechanism public interface GreatService { @GET("/repos/{owner}/{repo}/contributors") Call> repoContributors( @Path("owner") String owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") Observable> repoContributors2( @Path("owner") String owner, @Path("repo") String repo); @GET("/repos/{owner}/{repo}/contributors") Future> repoContributors3( @Path("owner") String owner, @Path("repo") String repo); }

Slide 13

Slide 13 text

Pluggable execution mechanism Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build();

Slide 14

Slide 14 text

Pluggable execution mechanism Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build();

Slide 15

Slide 15 text

Future • Parameter handlers • Logging? • Finalizing mock module • Documentation • WebSockets!(in v2.1)

Slide 16

Slide 16 text

more info • Simple HTTP with retrofit2 • https://www.youtube.com/watch? v=KIAoQbAu3eA • sample code • https://github.com/punchdrunker/ Retrofit2Sample

Slide 17

Slide 17 text

Thank you!