Home > Articles > Programming > Java

Accessing Web Services Through Android Apps

  • Print
  • + Share This
Bintu Harwani, author of Android Programming Unleashed, describes how to use publicly available web services to access and return all sorts of useful information on the Web via smartphone. Why write your own code to do everything your user needs, when someone else has already put in the time to construct, test, and make available these handy labor-saving services?
From the author of

In this article, I'll discuss how to access publicly available web services via Android applications. To learn how to do that, we first need to take a quick look at the basics of web services.

How Web Services Work

A web service is a technique by which two applications can communicate with each other, regardless of platform or programming language. The web service usually requires some data or argument to be passed to it; the service performs some kind of process(es) on that data, and finally the web service returns the data in a specific format defined in the web service's programming.

Web services are a useful shortcut for adding the maximum number of features to any web application in the shortest possible time. Instead of building a feature for any application from scratch, in many cases a better option is to invoke a publicly available web service directly from your application.

Suppose I want to provide a search facility that visitors to my website can use. I could build my own search application from scratch, but that would be a lot of work, ranging from building a database of information to developing various indexing and hashing features. Instead, I can use the Google Search web service on my website. The web service accesses Google's info database in response to the visitor's search, and it returns the requested results to the visitor.

A web service consists of several methods that are advertised for use by the general public. To make it possible for every application to access a web service, these services use web service protocols, including REST, SOAP, JSON-RPC, JSON-WSP, XML-RPC, and so on. A web service can exchange data in any format, but two formats are the most popular for data exchange between applications:

  • XML. Standard format for data exchange among applications, in order to avoid datatype-mismatch problems.
  • JavaScript Object Notation (JSON). Text-based open standard for representing data. Uses characters such as brackets ([]), braces ({}), colons (:), and commas (,) to represent data.

In this article, we'll focus on the JSON data-exchange format. Data in JSON is represented using simple key/value pairs, with more-complex data represented as associative arrays. For example, strings in JSON representation look like this:

["bintu", "bmharwani@yahoo.com",...]

The information can also be represented in the form of key/value pairs:

{"name" : "bintu", "email": "bmharwani@yahoo.com"}

In the example above, name and email are keys, and bintu and bmharwani@yahoo.com are the keys' respective values. Many sites provide web services that return JSON-formatted data.

We'll be looking at how to access the weather information provided by the OpenWeatherMap web service through an Android application. The OpenWeatherMap service provides a free weather data and forecast API. After the user provides city and country information, the web service returns the current weather information in JSON-formatted data. The web service is accessed using the following syntax:


where city_name and country_name can be replaced with the city and country names whose weather data we want to fetch. For example, when we point the browser to the following URL, the web service will deliver weather information for the city of Chicago, Illinois:


We can see that the URL specifies Chicago and the USA as the city and country names, respectively. The weather data returned by the web service will be in JSON format, as shown below:

{"coord":{"lon":-87.632446289062,"lat":41.884250640869},"sys":{"country":"United States
 of America","sunrise":1376391452,"sunset":1376441573},"weather":[{"id":800,"main":
 "Clear","description":"scattered clouds","icon":"01n"}],"base":"global stations",
 "main":{"temp":293.15,"humidity":53,"pressure":1017, "temp_min":294.04,"temp_max"

In the returned data, we can see that the attributes for latitude, longitude, temperature, humidity, pressure, and so on are assigned the values that match the current weather status of the supplied city.

  • + Share This
  • 🔖 Save To Your Account