7 Retrofit - G00fY2/android-mvp-wiki GitHub Wiki

7 Retrofit

Retrofit is a type-safe REST client for Android developed by Square. HTTP requests to webservices are represented as interface methods. Retrofit supports converter which can parse JSON data (or other structured data) into Java objects (Model classes). Retrofit also supports adapters for executing the Call instances. This allows us to seamlessly use Retrofit together with RxJava.

7.1 Classes

Model classes (POJOs)

  • used to allow type-safe return types
  • represent the data from the response
  • can be used to send data (e.g. POST and PUT requests)

Interface

  • a Java interface class
  • defines the HTTP operations / API calls for the related webservice
  • makes use of annotations for specify the request type (@GET("relative URL")) and the relative URL
  • allows URL manipulation by replacing placeholders in the URL with @Path("placeholder") annotation
  • offers @Query("param") annotation to dynamically set the URL query
  • model classes can be specified as Body with method(@Body Class name)

Retrofit.Builder

  • creates an instance of the defined interface
  • here you define the base URL, converter and adapter