Android Restful web services tutorial

In this tutorial, we are going to integrate android with restful web services which return json as response.
So we are going to get json from restful web services and then render json response to android custom listview.

Example :

I have already implemented restful webservices json example. I am going to use same example for implementation of restful web services.
If you are going to deploy above web service then use below url to fetch json data

Here 192.168.2.22 is IP address of my machine. Once you implement this web services on your machine, you should replace it with your ip address.
If you don’t want to implement rest web service yourself and want below json response. You can use below link.
https://cdn.rawgit.com/arpitmandliya/AndroidRestJSONExample/master/countries.json
You will get below Json response from above URL:

We are getting above Json array from rest web service. If you are not familiar with JSON, you can go through Json tutorial. We are going to render above JSON response to android custom listview as below:

Lets code this example now:

Source code:

Step 1 :Creating Project

Create an android application project named “AndroidRestJsonExample”.

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:

We have one button and listview. When you click the button, we are going to populate data in listview by calling restful web services and render it on the listview.

Step 3: Creating layout for Row

As We have declared ListView widget in activity_main.xml. Now we need to provide layout for individual row.
  • Go to res -> layout
  • right click on layout
  • Click on New -> File.
  • Create a file named “row_item.xml” and paste below code in row_item.xml.

Step 4 : Creating model object Country :

If you notice, we are getting Json array from restful web service which has 4 items. Each item have two attributes id and countryName. We are going to create country object for each item.

Step 5 :  Creating BaseAdapter for ListView

Before creating MainActivity, we need to create CustomCountryList class for custom ListView row.
This class is used to populating data for ListVIew. getView method is get called for drawing each row.

Step 6 : Creating MainActivity

We are going to create a addition inner class GetServerData which extends AsyncTask. This class will make https call in background thread.
Here are three methods which you may need to implement while using AsyncTask.

  • onPreExecute() : This method will get called before making HTTP call. We are initializing ProgressDialog here.
  • doInBackground : We are going to make HTTP call in this method. We have use HttpURLConnection class to make HTTP call. You can not access any UI elements while executing this method
  • onPostExecute  : This executes after execution of doInBackground method. In this method, we are going to create CustomCountryList object and populate listview data.

Change src/main/packageName/MainActivity.java as below:

Step 7: Add internet permission in AndroidManifest.xml

Copy following code:

Put it in AndroidManifest.xml

Done, we have added internet permission to AndroidManifest.xml. Your application must be able to access internet now.

Step 8 : Running the app

When you run the app, you will get below screen:

When you click on above button, you will get below screen.

We are done with Android Restful web services json example. If you are facing any issue, please comment.

Was this post helpful?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *