Using Retrofit 2 for web-services in Android with a simple demo project.
So, What is Retrofit?
Retrofit is type-safe HTTP client for Android and Java by Square, Inc. It is an open source library which simplifies HTTP communication by turning remote APIs into declarative, type-safe interfaces. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. It automatically serializes the JSON response using a POJO which must be defined in advanced for the JSON Structure. Get full project in GITHUB.
Rest API and JsonObject Model
REST APIs is often used in mobile applications, social networking Web sites, mashup tools, and automated business processes. The REST style emphasizes that interactions between clients and services is enhanced by having a limited number of operations. Using this link https://api.myjson.com/bins/1bsqcn/ we are trying to fetch the json data. The JsonResponse will be like:
Here, I have described the basics of retrofit just in 8 steps. After completion of this project the result would look like.
Project Structure in AndroidStudio.
Step 1: Add gradle dependencies
The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. To serialize JSON we need a converter to convert it into Gson first. We need to add the following dependencies in our
Step 2: Add uses-permission to Manifest.xml
Retrofit performs HTTP requests against an API running on a server somewhere in the Internet. Executing those requests from an Android application requires the Internet permission to open network sockets. Now, In order to perform network operations in our application, our manifest file must include the following permissions:
Step 3: Creating Layout files
A layout resource defines the architecture for the UI in an Activity or a component of a UI. So, now we need to create our layout resources file activity_mail.xml and content_main.xml for MainActivity.java and single_row_view.xml to present the views in the recycler view.
Step 4: Creating POJO
POJO is an acronym for Plain Old Java Object. POJO class acts as a bean which is used to set and get the value.
@SerializedName annotation is used to specify the name of the field that’s in the JSON Response.
Step 5: Setting Up the Retrofit Interface
The getRetrofitInstance() method in the code will be called every time while setting up a Retrofit interface. Retrofit provides with a list of annotations for each of the HTTP methods:
@GET, @POST, @PUT, @DELETE, @PATCH or @HEAD
Step 6. Create interface to fetch JsonApis
Define the REST API for Retrofit via the following interface. The following code defines the GetNoticeDataService and a method getNoticeData to request the list of notice for a given user. The
@GET annotation declares that this request uses the HTTP
GET method. The code snippet also illustrates the usage of Retrofit’s path parameter as (“bins/1bsqcn/”).
How to Describe API Endpoints? URL Manipulation just for knowledge(Not using in this project)
Step 7: Create Adapter
Now, we will create an adapter that is used to display the list of notices the in the recycler view.
Step 8: MainActivity.java
Now, In your activity.java class first create the retrofit instance service and use that service to fetch data by using the interface. Here you don’t pass your callback as the last parameter. You use the
service to get a
call object. Once you’ve invoked
.enqueue on the created
call object the request will be made by Retrofit. And you should be able to make your very first request with Retrofit. If everything went well, Retrofit will return you a
Now after running the app in the emulator the output will looks like. Get the project source code at GITHUB.
And lastly, congratulation you learned the basics of RETROFIT. Happy Coding. If you find this article is helpful please do not forget to share, recommend and clap.