How to combine rxjava2 and retrofit2 to make restful api in Android

Posted by

Introductions

RxJava and Retrofit is so popular in Android platform. Today I am going to show you of guys. How to combine RxJava2 and Retrofit in order to make restful api request. Before You start to jump into this tutorial. You need to know some concept about RxJava android Retrofit. If you have not known yet.  with RXJava or Retrofit you can visit there to know more how it works. otherwise you can skip those step and continue doing next step.

Let save time I already collections RXJava2 and Retrofit into one small library you can go there tpcreative libraries  to know How to initialize lib into your project.

Are you ready ? Let get started.

Download Now

 

  1. Creating project and fit full up info. File->New->New Project
  2. Configuration and init into project.
    • At root of your project you need to edit build.gradle

       
    • At your src take a look at build.gradle you will add some snippet codes


       
    • Creating your flow folder
    • In order to init RxJava android Retrofit you need to initialize Application at ui->user package

       
    • At common package you will create API.class where to call api from your server

       
  3. Integrating MVP.
    • In previous tutorial MVP architecture pattern in Android you can go there.
    • In this tutorial you need to create 3 activities (SiginInActivity, SiginUpActivity and HomeActivity)
    • You also have UserPresenter, UserView, User and Navigator.
    • The first of all Let mvp can work with activity or fragment. you will create UserView

       
    • At model you are going to User let them can interact with Retrofit in this tutorial I am using Gson will be auto parsed from json to Object.

       
    • Creating params to send data your server common->request LoginRequest.java

       
    • RegisterRequest.java

       
    • LoginResponse.java

       
    • Come back your user package ui->user you need to create UserPresenter.java

       
    • Take a look at the class above. You will see onSignIn and onSignUp for the both func was combined with RXJava and Retrofit that’s all for MVP, RXjava and Retrofit => Restful api.
  4. Creating Activity  and UI
    • At ui -> user package click on package name user New ->  Activity -> Empty Activity(SignUpActivity) and check Generate Layout file.
    • After you will 2 files SignUpActivity.java activity_signup.xml and then continue doing the rest of activity SignInActivity and HomeActivity.
    • At SignUpActivity you will edit code like this

       
    • activity_signup.xml

       
    • SignInActivity.java

       
    • activity_signin.xml

       
    • home->HomeActivity.java

       
    • activity_home.xml

       
    • At common package -> Navigator.java

       
  5. Editing AndroidMainifest
    • Declare permission to call internet in android

       
    • Running and getting great result
  6. Overview
    • UI(xml) you are using custom widget and font
    • Presenter and Activity will work together. every time activity send a request to presenter and then presenter will call api to your server after response back to Activity via UserView.

 

Conclusion

By the way you can totally overview MVP and RXJava, Retrofit works together. especially with kind of model they fit big project and complicated func and beside easy to maintain in the future. When you have been working with a team.so that some members can join into one project because infrastructure is clear that’s best advantages to allow you do that.

 

Leave a Reply

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