Chapter16 Web Services - Publish
Chapter16 Web Services - Publish
OVERVIEW
CLIENT SIDE – CONSUMING WEBSERVICES
EXAMPLES
MOBILE DEVELOPMENT
CONSUMING WEB SERVICES USING SOAP AND REST APPS
OVERVIEW
OVERVIEW (Advantages of using the webService architecture)
A WebService is a Consumer_Machine-to-Provider_Machine collaboration schema that operates
over a computer network.
Under the WebService strategy the invoked functions
The data exchanges occur independently of the OS, browser, platform, and programming
are implemented once (in the server) and called many
languages used by the provider and the consumers. times (by the remote users).
A provider may expose multiple EndPoints (sets of WebServices), each offering any number of
typically related functions. Some advantages of this organization are:
WebServices expect the computer network to support standard Web protocols such as XML, ◦Elimination of redundant code,
HTTP, HTTPS, FTP, and SMTP. ◦Ability to transparently make changes on the server to
Example: Weather information, money exchange rates, world news, stock market quotation are update a particular service function without clients having
examples of applications that can be modeled around the notion of a remote data-services
provider surrounded by countless consumers tapping on the server’s resources.
to be informed.
◦Reduced maintenance and production costs.
OVERVIEW
(Why should Android developer learn how to
create a WebService?) OVERVIEW (WEBSERVICE ARCHITECTURE)
Simple apps are usually self-contained and do not need to collaborate with other parties to An ideal Webservice provider is designed around four logical layers which define the ways in
obtain additional data or services (for instance, think of a scientific calculator) which data is to be transported, encoded, exposed and discovered by the users.
However, there are many cases in which the data needed to work with is very extensive, or Layers Responsibility
changes very often and cannot (should not) be hard-coded into the app. Instead, this kind of data
should be requested from a reliable external source (for instance, what is the Euro-to-Dollar rate Transport Move messages through the network, using HTTP, SMTP, FTP, …
of change right now?) Messaging Encoding of data to be exchanged (XML)
Another class of apps requires a very high computing power perhaps not available in the mobile Description WSDL (Web Service Desc. Lang) used for describing public methods available from the
device (think of problems such as finding the shortest/fastest route between to mapped endpoint
locations, or best air-fare & route selection for a traveler) Discovery UDDI (Universal Description & Discovery Integration) facilitates location and publishing of
services through a common registry
It is wise for an Android developer to learn how to solve typical problems that exceed the
capacities of the handheld devices. Understanding the possibilities offered by the client-server
computing model will make the developer be a more complete and better professional.
CLIENT SIDE – CONSUMING WEBSERVICES CLIENT SIDE – CONSUMING WEBSERVICES
There are two widely used forms of invoking and consuming Example: Using REST. The following URL is used to make a call to the Google Search service
WebServices: asking to provide links to the subject “Cleveland State University”
◦ Representational State Transfer (REST): Closely tie to the HTTP protocol by https://www.google.com/search?q=cleveland+state+university
associating its operation to the common methods: GET, POST, PUT, DELETE for
HTTP/HTTPS. This model has a simple invocation mode and little overhead. Transport Provider Action Arguments
Service calls rely on a URL which may also carry arguments. Sender & receiver
must have an understanding of how they pass data items from one another. As
an example: Google Maps API uses the REST model.
◦ Remote Procedure Call (RPC): Remote services are seen as coherent
collections of discoverable functions (or method calls) stored and exposed by
EndPoint providers. Some implementations of this category include: Simple
Object Access Protocol (SOAP), Common Object Request Broker Architecture
(CORBA), Microsoft's Distributed Component Object Model (DCOM) and Sun
Microsystems's Java/Remote Method Invocation (RMI).
◦ REST users refer to their remote services through a conventional URL that
function-name, arguments.
Web-Client Response: XML formatted results WSDL Exploration Tool
commonly includes the location of the (stateless ) server, the service name, REST
Using common URL
the function to be executed and the parameters needed by the function to Request
http://provider.org?op=function& arg1=val1& arg2=val2
operate (if any). Data is transported using HTTP/HTTPS. Response
Free format. Options include: Plain-text, HTML, XML, JSON…
◦ SOAP requires some scripting effort to create an XML envelop in which data
travels. An additional overhead on the receiving end is needed to extract data
from the XML envelope. SOAP accepts a variety of transport mechanisms,
among them HTTP, HTTPS, FTP, SMTP, etc.
◦ SOAP uses WSDL (WebService Description Language) for exposing the format and operation of the
services. REST lacks an equivalent exploratory tool.
EXAMPLES OF ANDROID APPS USING REST & SOAP WINDOWS COMMUNICATION FOUNDATION (WCF)
In the next sections we will present three examples showing how an WCF is a Microsoft technology that provides a framework for writing
Android web-client typically interacts with a remote server code to communicate across heterogeneous platforms [1, 2].
requesting and consuming WebServices. ◦ 1. An IIS WebServer may host various EndPoints (WebServices).
◦ Example 1. SOAP client / .NET provider: An Android app uses a XML KSOAP
envelope to call a Windows IIS server. WebServices are implemented as a set ◦ 2. Each of those EndPoints uses WSDL to provide a way of exposing its
of C#.NET functions. composition and behavior to clients wishing to find and
◦ Example 2. REST client / PHP provider: A REST Android client invokes remote communicate with the services.
PHP services which consult a database on behalf of the client. The response is
formatted using JSON. ◦ 3. Each endpoint includes:
◦ Example 3. REST client / Servlet provider: Our Android app communicates with ◦ address (URL - where to send messages), WCF ASMX
an Tomcat Server in which its WebServices are implemented as Java Servlets. ◦ binding (how to send messages ), and a
As in the previous example, the results of a database query are returned as a ◦ contract (an explanation of what messages contain)
JSON string.
https://docs.microsoft.com/en-us/archive/msdn-magazine/2006/february/learn-the-abcs-of-programming-windows-communication-foundation
https://docs.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial?redirectedfrom=MSDN
WINDOWS COMMUNICATION FOUNDATION
(WSDL service contracts) WINDOWS COMMUNICATION FOUNDATION (WCF)
Example: Removing “?WSDL” from the links will exposes endpoint service functions
SOAP
(Request) Envelope with RESPONSE
HttpUrlConnection XML <tag>…..</tag> Web-
DATA
Services BASE
Incoming Envelop
(Response)
}
result.setText((String) msg.obj); OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8);
writer.write(reqXML);
“ <s:Body>\n” +
}; writer.flush();
@Override if(con.getResponseCode() == HttpURLConnection.HTTP_OK) { “ <GetData xmlns=\“http://tempuri.org/\”>\n” +
protected void onCreate(Bundle savedInstanceState) { byte[] byteBuf = new byte[1024];
“ <value>” + 2 + “</value>\n” +
super.onCreate(savedInstanceState); InputStream resStream = con.getInputStream();
setContentView(R.layout.activity_main); int resLen = 0, len = resStream.read(byteBuf);
result = (TextView) findViewById(R.id.txtResult); while (len > -1) {
Thread slowJob = new Thread() {
@Override }
resLen += len; resultValue += new String(byteBuf); len = resStream.read(byteBuf);
“ </GetData>\n” +
public void run() { }
String resultValue = “”; HttpURLConnection con = null; } “ </s:Body>\n” +
try { catch (Exception ex){ resultValue = “\nERROR: ” + ex.getMessage(); }
String reqXML = “…”; note
“</s:Envelope>”;
// send message to handler so it updates GUI
// Creating the HttpURLConnection object Message msg = handler.obtainMessage(); msg.obj = (String) resultValue; handler.sendMessage(msg);
URL oURL = new URL(“http://192.168.1.9/WcfService/Service1.svc”); if(con != null) con.disconnect();
con = (HttpURLConnection) oURL.openConnection(); }
};
slowJob.start();
}
}