Web Services In Android

There are two types of applications in the market first Static application and second Dynamic application. Static application has fixed and unchangeable content whereas Dynamic application change their content based on web services data. The static application can not take or process user data but the Dynamic application has this type of facility. These facilities are generally provided by Web services(also called hosting services). Web Services provides its Server to store or process the data. 

Introduction

It is a server-side platform that receives and process request from different types of application irrespective of language and platform.

Web services are an essential component when dealing with an application that is dynamic because most of the applications running these days use web services. Web services provide an application to host their data on the cloud (or servers).

All web services are API, but all APIs are not web services. Most people get confused about the minor difference between web services and  API. Let's understand this in very simple terms. If API is running on any web server then it is a web service or provides any kind of service over the web hence it is called a web service whereas web services can be of many more types like providing SAAS(software as a service), IAAS(infrastructure as a service), PAAS(Platform as a service) these all will provide services but API can also be present of offline like if we have to connect an application to another application within devices so there is no need of a server to be involved in that case.

So now let's have a look at API

API(Application Programming Interface)

An API act as an interface between two different application so they can communicate with each other.

  • API is a messenger which delivers the request to the provider and responds.
  • API can communicate over a network and also without a network.

                                   

Some popular APIs are as follows,

  • Google Map API
  • Youtube API
  • Facebook Login API
  • Gmail login API

How API helps Developers

An API helps developers in many ways some of them are as follows-

  • API helps developers to use the third-party application
  • Do not reinvent the wheel over and over.
  • Efficient product in a short period.

Types of Web Services

There are mainly two types of web services SOAP and REST. Let's understand each one in brief.

SOAP(Simple Object Access Protocol )

SOAP or Simple Object Access Protocol is a technique to send an XML request over the internet using HTTP protocol and return an XML response. Here envelope is used to send data hence its data transfer is secure. It was used in previous applications and it is a very secure protocol.

REST(Representational State Transfer Protocol)

Any Web Service that is defined on the Principle of REST uses HTTP verbs of getting, POST, PUT, and DELETE.

REST allocates resources on URL and acts. it allows multiple web service-based systems to interact and communicate with them.

SOAP REST
  1. This is a function-based protocol.
  2. It only uses XML.
  3. It can't be cached.
  4. It has a strict communication
  5. Build-in ACID compliance.
  6. used in banking applications where security is prior.
  1. This is Database based Protocol.
  2. This Permit HTTP,plain text,XML,JSON
  3. It can be Cached.
  4. It has an easy communication
  5. Lack of ACID compliance
  6. It is advance and simple

REST API mostly uses JSON format so let us see what is JSON.

JSON(JavaScript object Annotation)

It is not a programming language, it s a data interchange format.JSON is mostly used to get and post the data in web services. it is the mostly used formate in today's application development.

  • It is a file in which data is written in text formate.
  • it is easy and simple to understand

Data types of JSON

  1. String - "C-sharp Corner"
  2. numbers - 1,2,-1,-2
  3. Boolean - true,false
  4. Array - ["java","python","HTML"] or [2,6,-1]
  5. object - { "key1" : "value1",  "key2": "value2"}

Example of JSON formate, 

{
    "name": "ravi",
    "age": 23,
    "email": "[email protected]",
    "programming": ["java", "C++", "python"],
    "experience": [{
        "company Name": "Company1",
        "years": 2,
        "location": "noida"
    } {
        "company Name": "company2",
        "years": 2,
        "location": "noida"
    }]
}

How to Create an API in Android

here is a simple Connection API that is used to connect and authenticate our application to web services 

<?php 
    $hostName = 'localhost';
	$userName = 'root';
	$userPass = '';
	$dbName = 'mydb';
	$con = mysqli_connect($hostName,$userName,$userPass,$dbName);
	if (!$con) {
		echo "connection failed";	
	}
	else
	{
		echo "connection successful";
	}
?>

Conclusion

In this article we have learned what is web Services in android and what is API, we have covered the difference between web services and API and after that, we have learned types of Web services SOAP and REST. After that, we have seen the table of REST and SOAP differences, and at last we have seen the JSON format and a simple Connection API.


Similar Articles