TIL: Retrofit2

Discovering, using and loving Retrofit in PhotoPress.

Got the starter boilerplate code from Retrofit website, and updated a couple of methods for the WordPress.com API. Got it to work, then started expanding the interface with more methods and data classes to flesh it out more.

It’s a delight to work with—simplifies things a lot by taking care of request creation, error handling, and response parsing. It has also helped make the API calls a lot more robust.

I’m already planning to convert AcceleReader for Instapaper to Retrofit as well! It’ll take more work because of the slightly wild-west nature of Instapaper API, but should be doable, and good for the code (and my learning).

Though, like all things Android dev, it has its quirks too. Some examples I’ve encountered so far:

  1. Can’t pass a POJO/data class object as a query parameter directly. Either have to pass all parameters individually, providing names, or as a Map of parameter names and values. Just adds the extra hassle step of converting POJOs into a Map just to pass as argument.
  2. Specifying a @Part in a @Multipart request as String directly in the method arguments causes the string to be sent as a quoted string. It’ll arrive at the other end as "This string". Send the same string as a MultipartBody.Part makes it go as a normal string, arriving as This string. Weird.