Web Services

In the previous tutorial, you have learned about the basics of Google Maps API integration in Android Application. In this tutorial, you will learn the basics of web services.

Introduction

According to W3C, a web service is a software system which supports the machines interaction over the network. Other systems use their features by interacting through an interface described in the machine-processable format.  This process is typically followed by HTTP with an XML serialization in conjunction with other web standards.

Web Service is calling a method, procedure or function which is running on the different machine known as a server. The service can be any other machine than the client.

Why you need web services?

When you need some particular general purpose task to do, and if there is already an implemented functionality of that specific task as a service then you can use a web service rather than you implement the complete functionality. You can also get data from another web component to use it in your application. It is a predefined application component made for others. In short, you can use it to overcome interoperability problem by providing the ability to the applications and platforms to share their data and resources.

 Web Site vs. Web Service

The main difference between Web Site and Web Service is

A Web Site is intended for human consumption i.e. Human to Computer Interaction. For example, when you need some information or data, you browse the internet and interact with the computer (Server) to get information.

A web service is intended for Computer to Computer interaction. For example, when the computer needs some information or data, it interacts with another computer which is providing web services.

Web Service

There is an exchange of messages when a web service and a client communicate with each other.

Client and Web Service Communication

First of all the client sends a request message to the web service. As a result web service responds with a response message. This process is similar to the simple HTTP where the browser sends HTTP request to the server, and the server then replies with an HTTP response.

Web Service Message Formats

SOAP

In the beginning, the only supported web service message format was SOAP (Simple Object Access Protocol) which is now deprecated to some extent, but many old deployed systems are still using it. SOAP is XML based web service message format.

SOAP is wrapped in a container called Envelope. It has a header and a body. The body contains the data in SOAP.

REST

REST (REpresentational State Transfer) is the newer web service message format. These web services use plain XMLplain XML, JSON (JavaScript Object Notation), plain text, or you can also use customized forms in the web services.

Working of REST

Clients make requests through HTTP GET and POST methods. Servers respond in the form of XML, JSON, Plain Text or it can be in customized forms. The status code in the REST services indicates the outcome.

REST + XML

REST + JSON

XML

XML (EXtensible Markup Language) was designed to store and transport data and to be both human and machine-readable.

JSON

JSON (JavaScript Object Notation) is a text-based lightweight open standard used for human-readable data interchange. It is derived from JavaScript scripting language. It represents simple associative arrays and data structures known as objects. In spite of its relationship with JavaScript, it is language-independent as it has parsers available for a lot of languages. If you want to learn more about JSON, see JSON’s official website.

JSON vs. XML

  • XML is easier to read whereas JSON is harder to read.
  • The industry is using XML a lot more than JSON.
  • XML is one of the primary industry standards whereas JSON is rising in the ranks as one of the industry standards.
  • XML takes up a lot more space as compared to JSON.
  • XML uses more bandwidth because of more size whereas JSON uses less bandwidth because of its small size.

Syntax Rules for JSON

Here are some basic rules you must follow while writing JSON

  1. Data must be placed in the form of Name/Value pair.
  2. Each data entry is separated by a comma.
  3. Curly brackets contain objects and Square brackets contain arrays
  4. JSON has Number, String, Boolean, Object, Array, and null as data types.

JSON Representation Syntax Example

Web Service Providers

Most of the online businesses, services, portals and many social networks provide web services API to the developers. Some of the free web services for developers are REST Countries, Free Currency Converter API, Open Weather Map, etc.

You can find more web services by searching google or ProgrammableWeb.

There are also several Offline / Online / Plugin tools are also available to parse and Create JSON, Some online JSON tools are JSON Viewer, JSON Editor Online, and JSON Parser Online.

Learn more about JSON.

Retrofit with Gson tutorial to get Countries data from Rest Countries API

Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!