Skip to main content

What is Retrofit?

You might be knowing that in almost every Android project we need to work with APIs, which is nothing but an HTTP call to a particular URL. And we have already done this thing in some previous posts, an example you can see from the below-given link.
If you have been through the above post, then you know how much code we need to write for the network operation. And at the same time, we need to take care of a hell lot of things, like memory, cache, etc. So to make the life easier we have some libraries that can simplify our task. And Retrofit is one of the most popular used libraries for this job.

The API

The URL that gives us some data or to some task upon sending an HTTP request is called an API. And for this example, I already have an API, so you do not need to create the API. Below is our API URL.
https://simplifiedcoding.net/demos/marvel/
If you go to the above URL, you will get a response as shown below.
I hope you know about the JSON. So the response we are getting from the above URL is a JSON Array []. If you want to know how to make an API, you can see a detailed post about it from the link given below.
Now our task is to fetch and parse the above JSON data in our Android Application using the Retrofit Library. So let’s begin.

Retrofit Android Example

Creating a new Project

  • Lets first create a new Android Studio Project. I have created a project named RetrofitExample.
  • Once the project is created we need to add the following two libraries to our project.
    • Retrofit -> For the network calls
    • Gson -> For easy parsing of JSON data

Adding Retrofit and Gson

  • Adding libraries are very easy. You just need to add the following two lines inside the dependencies block of your app level build.gradle file.
  • After adding the lines just sync your project and you are done.

Creating API Interface

  • To make an API call using retrofit we need a java interface where we define all the URLs with the http request type and parameters. In this example we need to perform an http GET with no extra parameters.
  • So lets create a java interface inside and name it Api.java (You can name it anything you want).
  • As you can see we have a very simple interface above. Inside the interface first we have a BASE_URL. It contains the ROOT URL of our API. For any project we make an API like myproject/api/v1/apiname. Retrofit divides it in two parts the first part is the base URL and then the api name. So in our example marvel is the api name and before it we have the BASE URL
  • After the base URL we have an annotation @GET(“marvel”). It means that we are defining an http GET request. And the String passed inside the get is the api name. So for this GET it will make the complete URL joining the BASE_URL and the api name.
  • Then we have a method named getHeroes() whose return type is Call. And the type of the Call is a List<>. And type of the List is Hero. It may sounds confusing but it is not. Actually we need to structure the api call according the the response. So our URL is giving us an json array and that is nothing but a list of heroes. So we defined the Call type as a List and the List type as Hero. Now we need to define the Hero class.

The Model Class

  • Lets create a class named Hero. It will be a simple java class with a constructor and getters. And inside this class we will define all the attributes of our response.
  • Make sure your variable names matches with the JSON attributes. As we have the following JSON objects in our response.
  • So for getting this name we have to name our String as name (case sensitive) in the model class. Now if you want to make different names then you can define your model class as below.
  • So if you put the annotation @SerializedName(“attribute name”) with the json attribute name just above every variable you are defining, you can name your variables anything but you have to put the correct attribute name inside SerializedName(“name”).

Making the API Call

  • Now the final step is performing the API call. It is also very easy.
  • Now lets do it in our project.

Displaying the Heroes in a ListView

Creating Interface

  • Come inside activity_main.xml of your project and create a ListView.

Fetching the JSON Data

  • Now inside MainActivity.java write the following code.

Adding Internet Permission

  • As we are performing a network operation, internet permission is needed. So inside your AndroidManifest.xml define the internet permission.
  • Thats it now you can play your application.
retrofit android example
Retrofit Android Example
  • Bingo! it is working absolutely fine.

Displaying the Heroes in a RecyclerView

  • Here I showed only the hero names in a simple ListView, but you can use all the attributes to display. If you are confused how to do this? Here is one more tutorial doing the same with the volley. So once you got the List of heroes in your app, you can implement the process shown here.
  • I am using the same api in the link given above so you have to exactly the same thing to display the data in a RecyclerView.

Retrofit Android Example Source Code

  • If you are still confused you can get my source code from the link given here.
So that’s all for this Retrofit Android Example friends. Hope you liked it. So if you think it helped you then you, please help us by SHARING this post. Thank You đź™‚

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap then to bytes. Also

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App §   2.4.7  Set the color §   2.4.8  Adding Animations o    2.5  To plot grouped bar chart §   2.5.1  Creating Dataset o    2.6  Get the best of Android plus exclusive deals and freebies in your inbox!