Skip to main content

JSON Rest API in Android

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?

According to

REST is acronym for REpresentational State Transfer. It is architectural style for distributed hypermedia systems and was first presented by Roy Fielding in 2000 in his famous dissertation.

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.

For demo purposes, we are using user API it is free to use.

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.

  • onCreateViewHolder
  • onBindViewHolder
  • getItemCount
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.

See Also: Test your app without Android Emulator

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.

Now run your app and launch it and wait for some time depending on your internet connection speed and you will see your data will be displayed.

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 :)

Popular Posts

How to make splash screen in Android in 2020 [Best Guide]

Custom Switch in Android | | Android Tutorial

Working with Bottom Navigation bar in Android [Full Guide]