Building Live Stock Quotes App in Android

Web Services and Service Oriented Computing has revolutionized the internet in the past decade. It is quite true that without web services most Apps in the App Store will not be that interesting!!

Mobile devices have a limited processing power so it becomes necessary to let a web server do the heavy lifting when it comes to computation. So in this tutorial I am bringing you a way to use RESTful webservice in your android app and parse the XML response.

The example web service that I am using is here. This web service lets you access current price of Stock based on its trading symbol.

Step 1: Know Thy Web Service

Before calling any web service you must know how to call it and what you will get in response. This web service is a RESTful service so calling it is easy using a GET request.

GET /stockquote.asmx/GetQuote?symbol=string HTTP/1.1

Host: www.webservicex.net

The request requires a parameter that is the symbol for example AAPL is the stock ticker symbol for Apple Inc. and MSFT is for Microsoft. When we send a request like the following through a browser:

Step 2-Create thy app!!

Just open eclipse and follow the wizard!!

Step 3- The Manifest

We are accessing external web service so of course we have to add support for using the internet in the app. This means changing our manifest and adding the following line to the manifest inside the manifest tag:

<uses-permissionandroid:name=“android.permission.INTERNET”/>

Without this permission the app will not be able to contact the web service

Step 5-Calling Web Service

We will have to call the web service then parse the response. To do that I am creating a helper class which calls the service and returns various values. Initially I have added only basic getters and setters for the fields I want to display to my user.

Now we need to implement this particular event handler in our activity class called ChooseSymbolActivity. The handler will first check if the EditText we created contains a symbol or not before calling the AsyncTask we created