Using RxJava with Retrofit2

In this tutorial we will look how to use Retrofit2 and RxJava with each other. We will be using lambda expressions to reduce our boilerplate code. Have a look at this tutorial for setting up RetroLambda in your project.


[su_button url=”https://github.com/androidsrc/Retrofit2Sample” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Full Source Code[/su_b[/su_button]p> 

[su_yo[su_youtube url=”https://youtu.be/-zJkCTIBZYQ”]p> 

Retrofit2

Retrofit is the library through which your API interfaces are turned into callable objects. By default, Retrofit will give you sane defaults for your platform but it allows for customization.

It lets you use annotations to describe HTTP requests. It offers other customization like:

  • URL parameter replacement
  • Query parameters
  • Custom headers
  • Multipart request body
  • File uploads and downloads

In coming tutorials we’ll look at all of these in more detail.

Retrofit2 leverages OkHttp as the networking layer by default and is built on top of it. You don’t need to explicitly define OkHttp as a dependency for your project, unless you have a specific version requirement.

Preparing your project for Retrofit2

First of all we need to add dependency for retrofit2 in our app level build.gradle file. Retrofit2 requires a dependency for the automatic conversion of request and response payloads. Use the following dependencies if you’re using Retrofit2 and want JSON de-serialization using Gson:

GuRu

Technology enthusiast. Loves to tinker with things. Always trying to create something wonderful using technology. Loves coding for Android, Raspberry pi, Arduino , Opencv and much more.

You may also like...

1 Response

  1. Moh Halim Bimantara says:

    Very Helpful
    Thank You

Leave a Reply

Your email address will not be published. Required fields are marked *