Skip to main content

Using Retrofit 2 for web-services in Android with a simple

https://medium.com/cr8resume/make-your-hand-dirty-with-retrofit-2-a-type-safe-http-client-for-android-and-java-c546f88b3a51


 demo project.

So, What is Retrofit?
Retrofit is type-safe HTTP client for Android and Java by Square, IncIt 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 build.grade file.
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.
1. activity_main.xml
2. content_main.xml
3. single_row_view.xml
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.
  1. Notice.java
2. NoticeList.java
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 List<Notice>.
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.

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

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 § ...

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 th...