Android Programming – RESTful Web Service Questions and Answers

Q-1 What is web services? How it integrates and implements in industrial projects?

 

A web service is a standard used for exchanging information between applications or systems of heterogeneous type.

Software applications written in various programming languages and running on various platforms can use web services to exchange information over Internet using http protocol.

 

Why Web Service?

 

Expose method as a service over network:

  1. Web service is a chunk of code written in some programming language (Say C# or Java) that can be invoked remotely through http protocol.
  2. Once the Web methods are exposed publically, any applications can invoke it and use the functionality of the web methods.

 

Application Inter-operability – Connect heterogeneous applications:

  1. With the help of web service, heterogeneous applications (Java and PHP application) can communicate with each other
  2. Web service written in Java can be invoked from PHP by passing the required parameters to web methods.
  3. This makes the applications platform and technology independent.

 

Standardized protocol:

  • Web Services uses standardized industry standard protocol for the communication which must be adhered and followed by the applications creating Web Service.

 

Cost effective communication:

  • Web service is using SOAP over HTTP protocol for communication which is much cheaper than systems like B2B.

 

RESTFul Webservice Request and Response – Drill down

  • Fist step in designing RESTFul webservice is choosing the right domain name – say weatherinfo.org to retrieve weather information of cities
  • Let us take the first example (weatherinfo.org) which takes city name as input, composes weather information of the city and respond back to the browser.
  • Assume the response is in XML format, this can be in other formats like JSON as well.
  • Here is the structure of Request and Response:

 

HTTP Request The client request from the browser will look like:

GET http://weatherinfo.org/getweather/mumbai HTTP/1.1

HTTP Response The server response will look like

HTTP/1.1 200 Ok

Date: Mon, 14 Apr 2014 10:20:58 GMT

Content-Type: text/xml

Content-length: 139

 

<City name="Mumbai" datetime="2014-04-14 10:20:58 GMT" >

<Condition>Scattered Clouds</Condition>

<Temp>33</Temp>

</City>

 

  • Line 1 is the initial line which has the HTTP response code – 200 OK, lines 2 through 4 are the HTTP headers, line 5 is the mandatory blank line separating header and body, and lines 6 through 10 constitute the “HTTP Body (or content)” – this part is the data that the response carries and can be in any format, not necessarily XML.

 

Q-2 list out Advantages of using RESTful webservice.

 

  1. RESTful Web services are designed with less dependence on proprietary middleware (for example, an application server) than the SOAP- and WSDL-based kind.
  2. As per the RESTful interface design, XML or JSON over HTTP is a powerful interface that allows internal applications, such as Asynchronous JavaScript + XML/JSON (Ajax) – based custom user interfaces, to easily connect, address, and consume resources.
  3. The great fit between Ajax and REST has increased the amount of attention REST is getting these days
  4. Exposing a system’s resources through a RESTful API is a flexible way to provide different kinds of applications with data formatted in a standard way. It helps to meet integration requirements that are critical to building systems where data can be easily combined (mashups) and to extend or build on a set of base, RESTful services into something much bigger.