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

Retrofit

 Retrofit

How to rest right

Vlad Vysotsky

February 05, 2016
Tweet

More Decks by Vlad Vysotsky

Other Decks in Programming

Transcript

  1. Android Boilerplate Code - Databases (ORM, DAO) - Networking (JSON,

    data transferring) - UI templates - UI binding (view, saving states etc)
  2. Retrofit: Creators Type-safe HTTP client for Android and Java by

    Square, Inc Jake Wharton @JakeWharton http://jakewharton.com Bob Lee @crazybob http://crazybob.org http://square.github.io/
  3. OkHttp OkHttp is an HTTP client that’s efficient by default:

    • HTTP/2 support allows all requests to the same host to share a socket. • Connection pooling reduces request latency (if HTTP/2 isn’t available). • Transparent GZIP shrinks download sizes. • Response caching avoids the network completely for repeat requests. • OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.
  4. GSON Gson is a Java library that can be used

    to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
  5. GSON: From Java to Json (and vice-versa) public class Country

    { private String name; private int population; @SerializableName(“cities”) private List<String> listOfCities; private Government government; //getter and setter methods } class Government { @SerializableName(“president”) private String nameOfPresident; } { "name":"Ukraine", "population":46000000, "cities":[ "Kyiv", "Lviv", "Cherkasy" ], "government":{ "president":"Poroshenko P." } }
  6. RetroFit: Creating an Instance Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_ROOT)

    .addConverterFactory(GsonConverterFactory.create()) //or something else you need .client(new OkHttpClient()) //or already configured instance of the client .build();
  7. RetroFit: Services public interface GithubService { @GET("group/{id}/users") Call<List<User>> groupList( @Path("id")

    int groupId, @Query("sort") String sort ); } GithubService githubService = retrofit.create(GithubService.class)
  8. RetroFit: Using a Service Async githubService.groupList(1, “desc”).enqueue(new Callback<List<User>>() { @Override

    public void onResponse(Call<List<User>> call, Response<List<User>> response) { if (response.isSuccess()) { // 2** // do what you need } else { int statusCode = response.code(); // handle an error } } @Override public void onFailure(Call<List<User>> call, Throwable error) { // handle an exception } });
  9. RetroFit: Blocking Call try { List<User> users = githubService.groupList(1, “desc”).execute();

    } catch (IOException e) { // handle an exception } NOTE: Use it only if you’re not in main thread!
  10. What About LifeCycle? 1. Use Retrofit in loaders 2. Use

    it with RxJava 3. Use it with EventBus 4. Use it in another way created by your own imagination (but don’t forget about GC)
  11. RetroFit: Using with Loaders Actually, it would be great if

    you’re going to explore it by yourself like it’s your home task