How to setup Retrofit with RxJava and Gson

To get started you need to include the following in your build.gradle file:

Now you can set up your API interface with RxJava Observables, for example:

As you can see above, I can use my own data model and the JSON response will be converted automatically. I can also simply get a JsonObject if I just want to use the JSON myself.

Alternatively, I can set up my own deserializer, like so:

Lastly, here is my API creation with a deserializer for the User added in:

There are many different ways you can use Retrofit, RxJava, and Gson. This example can be used as a starting point, but there is much more you can do with each of these libraries if you need to.

Pierce Zaifman

Android app developer. Hates wasting time and takes satisfaction in improving efficiency in all aspects of life. Also enjoys helping people solve their problems.

London, Ontario, Canada

Subscribe to Pierce Zaifman

Get the latest posts delivered right to your inbox.

or subscribe via RSS.