In today’s tutorial we shall start off with Retrofit. We will be fetching list of employees by connecting to a service, which will provide employee data in JSON form and display it using RecyclerView and CardView. If your not familiar with using RecyclerView & CardView
please check out the previous post on these topics. So let’s begin
Step 1 : Add the following dependencies to your app level gradle
Step 2 : Add internet permission to Android Manifest.xml
Step 3 : Now add 4 sub packages activity, adapter, model and network. Move your MainActivity.java into the ‘activity’ package. Your project structure should look like the structure shown in the image below
Step 4 : The URL to get the JSON data is given below:
The JSON data retreived from this particular URL is as shown below
By analysing of the above JSON response we can see that it contains a JSON array name ‘employeeList’ which in turn contains multiple JSON objects each of which hold employee details.
To recieve this JSON we will need two model classes one for the JSON Array and the other for each JSON object.
Add two new file inside your model subpackage ‘Employee.java’ and the other ‘EmployeeList.java’. The content for both the files is given below:
Step 5 : Now create a new file in network package and name it “RetrofitInstance.java”. This file will provide the retrofit instance if it exists else it will create a new instance and return it.
Step 6 : Now add an interface into the same network package and name it “GetEmployeeDataService.java”. In this file we will use the @GET annotation provided by Retrofit Library, to which we will pass the end point or filename that will provide the JSON data.
Next we will create a method which will have return type of Call. In our case we will use the model class created earlier as type like Call.
We will also use the @Query annotation to attach the values sent to this method as url parameters in our case it’s company number. The contents of the file is given below
Step 7 : Now we will create layout file that will be utilized by the adapter of the RecyclerView to generate each item in the list. Add a new file to your ‘layout’ folder and give the filename as ‘row_employee.xml’. Add the following code to your file
Step 8: We will now create the adapter that will be used by the RecyclerView to display list of employees. Create a new java class in your adapter package and name it EmployeeAdapter.java
Step 9 : In the XML file activity_main.xml we will add the RecyclerView tag
Step 10 : Now in the MainActivity.java we will make an asynchronous network request to fetch the employee data. The JSON response will be available within the onResponse method. In case the request fails the onFailure method will be called. On request success we will then pass the ArrayList of Employee objects to the adapter to create a list.
Now run your android application you should be able to see the output as shown below:
If you enjoyed this article, please like and share…
Happy coding.
Comments
Post a Comment