Retrofit 2.0

Easy HTTP for Android

by Nick Fedesna

Android HTTP History 101:

What does Google recommend?

Google only has eyes for AOSP:
HttpURLConnection with AsyncTask

Udacity courses teach pure Java w/ AsyncTask
but does mention it's not really the best way.

Retrofit: Easy as 1-2-3

  1. Define API using an interface
  2. Create implementation with Retrofit
  3. Make async or synchronous Calls

Define API using an interface


        public interface GithubService {

            @GET("users/{user}/repos")
            Call<List<Repo>> listRepos(@Path("user") String user);

        }
            

Create implementation with Retrofit


    Retrofit retrofit = new Retrofit.Builder()
                                    .baseUrl("https://api.github.com")
                                    .build();

    GitHubService service = retrofit.create(GitHubService.class)
            

Make async or synchronous Calls


      Call<List<Repo>> reposCall = service.listRepos("octocat");

      reposCall.enqueue(new Callback<List<Repo>>() { … });

      Response<List<Repository>> reposResponse = reposCall.execute();
            











Jake Wharton, my (Java) hero!

Queries & Paths



    @GET("users/list?sort=desc")
    Call<List<User>> userListSorted();

    @GET("users/list")
    Call<List<User>> usersList(@Query("sort") String sort);

    @GET("group/{id}/users")
    Call<List<User>> groupList(@Path("id") int groupId);

    @GET("group/{id}/users")
    Call<List<User>> groupList(@Path("id") int groupId,
                               @QueryMap Map<String, String> options);
            

Request Bodies


    @POST("users/new")
    Call<User> createUser(@Body User user);

    @FormUrlEncoded
    @POST("user/edit")
    Call<User> updateUser(@Field("first_name") String first,
                          @Field("last_name") String last);

    @Multipart
    @PUT("user/photo")
    Call<User> updateUser(@Part("photo") RequestBody photo,
                          @Part("description") RequestBody description);
            
OkHttp3 RequestBody

Headers


    @Headers("Cache-Control: max-age=640000")
    @GET("widget/list") Call<List<Widget>> widgetList();

    @Headers({
        "Accept: application/vnd.github.v3.full+json",
        "User-Agent: Retrofit-Sample-App"
    })
    @GET("users/{username}")
    Call<User> getUser(@Path("username") String username);

    @GET("user")
    Call<User> getUser(@Header("Authorization") String authorization)
            
OkHttp Interceptors can add headers to all requests

Serialization Converter


        Retrofit retro = new Retrofit.Builder()
                .baseUrl("https://api.github.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
            
Gson, Jackson, Moshi, Protobuf, Wire, Simple XML, Scalars, LoganSquare

Pluggable Adapters


        Retrofit retro = new Retrofit.Builder()
                .baseUrl("https://api.github.com")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
            
RxJava, Guava, Java 8

RxJava modified interface:


        public interface GithubService {
            @GET("users/{user}/repos")
            Observable<List<Repo>> listRepos(@Path("user") String user);
        }
            

RxJava usage:


        api.listRepos("octocat")
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.immediate())
            .subscribe(list -> {
                // do something with repo list
            }, Throwable::printStackTrace);
            
demo: github.com/nick-fedesna/Retrofit2Demo

The End

by Nick Fedesna