Retrofit— A simple Android tutorial | JSON API Retrofit Library Working Tutorial

Friends! In today’s post, we will know what retrofit is and we will also know why it is necessary for network communication in Android.

Suppose I have a server in which some API is exposed, means that our server has some set of APIs methods, to save the data, to get the data. This means to consume this API from your mobile app. This means that my Android app wants to access or use this web server. So many APIs are HTTP based which means there will be communication between our Android apps and servers, it will be HTTP based. Means we will communicate using HTTP protocol.

So to do this communication, we have a library named retrofit. Moreover, there are different libraries to do networking communication. But retrofit is the most commonly used library.

So let’s know about this Retrofit library.

  • Library to fetch data from server
  • Volley is also another library to fetch data
  • Retrofit works well with JSON and XML both
  • Easy to Implement

Retrofit is a third party library. Through which we access data from the server. Generally, what happens in Android, we use a third-party library for all our common tasks. So retrofit is a good candidate through which we do networking.

Android also has a library named volley through which we are networking and accessing data from the server. But generally, people use retrofit. It works with different data formats. Such as JSON format or XML format. It works well with both. It is easy to implement, so generally, people use it.

So let’s see how it works.

Our android app sends a request to the server. Then the server first validates whether the user who sent the request is a valid request or not. And whether he is our authorized user or not. After validating, the server sends a response to our Android app. That will be a successful response or error response. If there is a successful response then our success will be a callback call. If the error is responding then error callback will be called.

In short, then our Android app will send the request to the server. The server will validate it. The server will then send a response to the Android app. The response will be either successful or error. If an error occurs then the error callback method will be called and if there is a success then the success callback method will be called. So this is how this whole cycle works.

Let us know how we will implement this in code.

Suppose there are too many endpoints inside my API that I can call. Such as the “GET USER” method to get users and “GETDATA” to get data and so on, “POST USER” to save users, etc. But it is not necessary to call our Android app, all of those methods. Maybe I want to call some set of methods only. So the retrofit says that you have to make an interface for all the methods you have to call. That is, define a structure so that the retrofit will know that you will call these methods.

So it is a simple concept to create an interface for all the API’s methods to call. Then after designing this interface, we will create the object of retrofit and that is what I want to call these methods. So the retrofit behind the scene will call HTTP for us and will bring a response. So we use retrofit in this way. There are only two steps, the first step is we define the interface. And then inside it defines the methods that I want to call.

Need of Converters and how does it works

Now it comes that our API, that JSON response returns. Meaning as soon as I call the endpoints, I will give a response in JSON. So the retrofit has done this thing. We do not have to pass a JSON response by ourselves. We will tell that this is our class and if you get a similar JSON, then you convert it to the object of java. Means that we will have converters, which will convert our JSON response into java objects. And as soon as we have objects, then we can do anything with them like we can bind them in recycler view. Or if data manipulation has to be done, you can.

So here is the simple concept of a retrofit. Now we understand it by code through or implement in code.

If you want to understand retrofit more thoroughly, or if you want to understand by code or by example, you can read our next post.

I hope you have liked this post very much and by now all your doubts will also be clear. And if there is any error in this post or you have to ask any other question, then you can give your opinion in the comment of this post.