Card image cap

Table of Contents:

  • Introduction
  • What is Retrofit?
  • An Example:
    1. Dependencies
    2. Permissions
    3. JSON data
    4. Model
    5. Retrofit Instance
    6. Define the Endpoints
    7. Recyclerview Adapter
    8. Initializing the Recyclerview
  • Final Thoughts


  • Introduction


    In this blog I'm gonna show you how to work with the retrofit in android.


    What is Retrofit?


    Retrofit is a type-safe HTTP client for Android and Java developed by Square.

    Using Retrofit we can easily translate the REST APIs to Java Interfaces. Retrofit can be used as the best alternative to Volley. Retrofit Uses OkHttp for making HTTP requests. Retrofit easily translates JSON or XML response to POJO’s (Plain Old Java Objects).


    Dependencies


    Open build.gradle (Module:app) and add Retrofit, GSON, RecyclerView and CardView dependencies.


    Make sure you have the latest dependencies. You can check them here


    Permissions


    Add the internet permission in your AndroidManifest.xml




    JSON data


    Now we will use sample JSON data from https://jsonplaceholder.typicode.com/posts

    which looks like this




    Model


    Make a model class Posts.java and put the following code inside it.



    Posts class has fields that represent the post title and body of a post. These fields are annotated with the @SerializedName annotation. The parameter (value) of this annotation is the name to be used when serializing and deserializing objects. For example, the Java field title is represented as a name in JSON.

    Later we've set getter and setter for all the variables.


    Retrofit Instance


    To issue network requests to a REST API with Retrofit, we need to create an instance using the RetrofitClientInstance class and configure it with a base URL. Create a class RetrofitClientInstance.java, here with the URL of our API we will make a call.




    Define the Endpoints


    Here we will provide the details about the parameters and request method.




    Recyclerview Adapter


    Now we will create the Adapter class for our recyclerview. Create a class RecyclerviewAdapter.java and paste the following code in it.




    Initializing the Recyclerview


    Finally, we will initialize the Endpoints interface and the Recyclerview in the MainActivity.




    That's All! Happy Coding!


    The complete code is available here.


    Final Thoughts


    One of the most important aspects of retrofit is the hassle-free setup provided by retrofit to integrate different converters used for data serialization. You can add GSON converters, XML converters or even custom converters.

    Need some extra help?.
    Check out the REST API course using Retrofit2 and MVVM architecture: here.




    Authors


    By Aayush Upadhyay

    www.instagram.com/aayush.5/

    Google Associate Android Developer


    Create an Account



    Have an account? Log In

    CodingWithMitch Members

    Unlimited access to all courses and videos

    Step by step guides to build real projects

    Video downloads for offline viewing

    Members can vote on what kind of content they want to see

    Access to a private chat with other communnity members & Mitch

    Become a Member

    CodingWithMitch Members

    Unlimited access to all courses and videos

    Step by step guides to build real projects

    Video downloads for offline viewing

    Members can vote on what kind of content they want to see

    Access to a private chat with other communnity members & Mitch

    Become a Member

    Comments