Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Retrofit Two Recap

punchdrunker
September 11, 2015

Retrofit Two Recap

punchdrunker

September 11, 2015
Tweet

More Decks by punchdrunker

Other Decks in Programming

Transcript

  1. • engineer at mixi, Inc • sidelines • DroidKaigi •

    teaching at schoo • Shibuya.apk • and so on… @punchdrunker
  2. Retrofit 2 way public interface GitHub { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> contributors(

    @Path("owner") Owner owner, @Path("repo") String repo); @GET Call<List<Contributor>> contributorsPaginate( @Url String url); } Parameter Type Dynamic Url
  3. 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<List<Contributor>> call = github.contributors(square, "retrofit"); Multiple Converter Call encapsulates single request/response interaction
  4. Asynchronous execution call.enqueue(new Callback<List<Contributor>>() { @Override public void onResponse(Response<List<Contributor>> response)

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

    { // Log.d(response.headers().toString()); // Log.d(response.raw()); // => okhttp/Response List<Contributor> contributors = response.body(); for (Contributor contributor : contributors) { Log.d(contributor.login + " (" + contributor.contributions + ")"); } } …. Parameterized Response Object
  6. 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
  7. Multiple Converter // We can user multiple converters public interface

    AwesomeService { @GET("/awesome/proto/endpoint") Call<ProtoResponse> awesomeProtoEndpoint(); @GET("/awesome/json/endpoint") Call<JsonResponse> awesomeJsonEndpoint(); } ————————————————————————————————————————————- Retrofit retrofitMulti = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(ProtoConverterFactory.create()) .build();
  8. Multiple execution mechanism public interface GreatService { @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); }
  9. Multiple execution mechanism public interface GreatService { @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); }
  10. more info • Simple HTTP with retrofit2 • https://www.youtube.com/watch? v=KIAoQbAu3eA

    • sample code • https://github.com/punchdrunker/ Retrofit2Sample