How to use JSON Rest API in Android App
Hello guys, Today we are going to see how we can consume JSON API in Android or we can say consume REST API in Android.
First, let’s see what is REST API?
So basically we can fetch data and create data on the server.
Now let’s make a working example on JSON API.
First, we have to create a new project in the android studio if you already not created it.
Our end result app will look like this.
To make this UI, we are using android recyclerview and cardview. To use them we need to add the dependency to our project.
After adding the dependency hit the sync button.
We need internet permission as we will fetch the data from the internet.
This is the single user object of user API.
Now, let’s open our activity xml file and add a recyclerview like this.
We have added the id to our recyclerview as userRecyclerView.
After that, we need to request the data through API using get method.
To do this we are using the library for networking.
Add this dependency to your project and hit the sync button.
Now open your activity java file and initialize android networking as shown below.
After that, we initialize our recyclerview.
To send get request from the android networking library we use this code.
In the onResponse method of JSONArrayRequestListener, if the get request return successfully then we will get our response in that method as JSON array.
In that method, first, we are checking if the response is not null and then we initialize our adapter for recyclerview, we will discuss adapter later in this post.
After initializing the adapter, we set the linear layout manager for the recyclerview then we set our adapter by using the setAdapter method and pass our adapter.
Now we have to make a single item layout file for our recyclerview. To do that, create a new layout file in the layout folder and name it as user_list_item.
in the user list item xml, we have added a cardview and in the cardview, we added 3 textview, 1 is for user’s name, 1 for user’s username and last for user’s phone number. See below code.
Now let’s create our adapter class.
Create a new java file in your package, we name it as UserAdapter and extend with RecyclerView.Adapter class and add our custom view holder generic which we have created within the adapter class as shown in the below code.
In the Adapter, we must implement these methods.
In the ViewHolder class, we have to create a construct by passing the view parameter. In the constructor, we are initializing all the items of our single-row item.
Now come to our adapter methods, to get data from our activity to adapter we created a construct and pass 2 parameters, 1st parameter is for the context of the activity and 2nd parameter is for the JSON array of response.
After that, in the onCreateViewHolder, we are returning the object of our view holder class UserViewHolder and in the parameter, we inflate our single row item layout file.
And in the onBindViewHolder, we are setting the values according to their fields and in the getItemCount method, we return the length of the user JSON array.
To send post request use this format.
If you have any questions or query you can mail me and if you like this article please share it with your friends and mates. Thank you for reading hope you learned something new.
Have a nice day 🙂