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

Retrofit, a simple REST client for Android

Retrofit, a simple REST client for Android

Wilfredo Nieves

November 26, 2016

More Decks by Wilfredo Nieves

Other Decks in Technology


  1. Retrofit, a simple REST client for Android Wilfredo Nieves Software

    Development Director @Optivon, Inc.
  2. @WillNieves19

  3. Optivon, Inc.

  4. Retrofit

  5. Used it?

  6. What is Retrofit?

  7. What is Retrofiit? • Type-safe REST client for Android •

    Model your remote API’s as interfaces. • Developed by Square • Made open source in 2010 (Retrofit 1) • Leverages Java annotations describe HTTP requests.
  8. What does it do? • Consumes remote API’s • HTTP

    requests (POST, GET, PUT, DELETE, etc.) • File uploads • Authentication • Serializes JSON or XML responses to POJO’s (Plain Old Java Objects)
  9. What we used before Retrofit? • Java Thread • Handlers

    • AsyncTask • Volley
  10. Performance

  11. Why use Retrofit • Easy to use • Wraps up

    Thread communication • Exception management. • Plenty of code examples on the web • Strong community
  12. What do we need? • POJO (Plain Old Java Object)

    • API service declarations (Java interfaces) • RestAdapter • android.permission.INTERNET Permission
  13. Plain Old Java Objects • Java Model class • Fields

    define a response from the API • Use the @SerializedName annotation to map response field name.
  14. Service Endpoints • Create a Java interface to describe your

    API endpoints • Use annotations to specify request type and headers • Callback parameter for asynchronous calls
  15. Other Annotations Examples

  16. Other Annotations Examples

  17. Other Annotations Examples

  18. Rest Adapter • Retrofit class adapts a Java interface to

    HTTP calls by using annotations • Build using the base URL of the API and a Converter object • Use the create method to generate an implementation
  19. Asynchronous Request • The Call class sends a request to

    a webserver and returns a response • Use the enqueue() method to make an asynchronous requests, execute() for synchronous. • To cancel a request just: call.cancel() • Clone instances with: call.clone()
  20. Response • Parameterized Response object • Response info. • Code

    • Success • Message • Headers • Raw body • Error body
  21. New in retrofit 2.0 • OkHttp is the default connection

    interface (is automatically added) • Pluggable JSON and XML converters, even create them yourself • Gson: com.squareup.retrofit:converter-gson Jackson: com.squareup.retrofit:converter-jackson Moshi: com.squareup.retrofit:converter-moshi Protobuf: com.squareup.retrofit:converter-protobuf Wire: com.squareup.retrofit:converter-wire Simple XML: com.squareup.retrofit:converter-simplexml
  22. Let’s write some code

  23. github.com/wnieves19/

  24. References • http://square.github.io/retrofit/ • https://futurestud.io/tutorials/retrofit-getting-started-and-android- client • https://instructure.github.io/blog/2013/12/09/volley-vs-retrofit/ • https://inthecheesefactory.com/blog/retrofit-2.0/en

  25. Thanks!