Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 66 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
66
Dung lượng
2,03 MB
Nội dung
Android
Consuming WebServices
Using
KSOAP (onIIS)andREST(onApacheTomcat)
18C
Victor Matos
Cleveland State University
Notes are based on:
Android Developers
http://developer.android.com/index.html
2 2 2
18C. Android - Internet WebServices
Consuming WebServices
2
Web Services
• Support machine-to-machine collaboration.
• They can be described, published, located, and invoked over a
data network.
• Webservices are used to implement the notion of a service-
oriented architecture (SOA).
SOA applications are independent of specific programming languages
or operating systems.
• Webservices rely on existing transport technologies, such as
HTTP, and XML, for invoking the implementation.
3 3 3
18C. Android - Internet WebServices
Consuming WebServices
3
Web Services
The interface describing the format of services can be done using
the WebServices Description Language (WSDL).
According to W3C there are two major types of webservices
• REST-compliant which use XML to represent its Web
resources, and offers a "stateless" set of operations; and
• Arbitrary solutions, in which the service may expose a
heterogeneous set of operations.
4 4 4
18C. Android - Internet WebServices
Consuming WebServices
4
Web Services - Implementations
Two widely used architectures supporting Webservices are
Representational State Transfer (REST) Closely tie to the HTTP protocol by
associating its operation to the common GET, POST, PUT, DELETE for HTTP.
Remote Procedure Call (RPC). Webservices are directly implemented as
language-specific functions or method calls. In this category we find
1. Object Management Group's (OMG) Common Object Request Broker
Architecture (CORBA),
2. Microsoft's Distributed Component Object Model (DCOM) and
3. Sun Microsystems's Java/Remote Method Invocation (RMI).
5 5 5 5
18C. Android - Internet WebServices
Consuming WebServices
5
Web Services
6 6 6
18C. Android - Internet WebServices
Consuming WebServices
6
How Android Applications Consume WebServices?
We will present two examples of how an Android application can
request services hosted in a
(1) IIS webserver (RPC discrete function oriented approach)
(2) Apache-Tomcat 7.0 (REST state oriented approach)
The examples are exhaustive and include details of how the
server-side methods are constructed (you may skip this portion
based on your previous experience)
7 7 7
18C. Android - Internet WebServices
Consuming WebServices
7
Example 1 - How .NET WebServices Are Called?
Services are passive server-side pieces of code waiting for
incoming messages to do some work. Clients initiate the interaction
by sending a message to server-services requesting action.
Services expose one or more endpoints where messages can be
sent. Each endpoint consists of
address (where to send messages)
binding (how to send messages )
contract (what messages contain)
Clients can use WSDL to know this information before accessing a
service.
8 8 8
18C. Android - Internet WebServices
Consuming WebServices
8
Example 1 - How .NET WebServices Are Called?
Windows Communication Foundation (WCF) uses the information
found in the service contract to perform dispatching and
serialization.
Dispatching is the process of deciding which method to call for
an incoming SOAP message.
Serialization is the process of mapping between the data found
in a SOAP message and the corresponding .NET objects used in
the method
9 9 9
18C. Android - Internet WebServices
Consuming WebServices
9
Example 1 - How .NET WebServices Are Called?
Our example code consists of two fragments which implement the
server and client side of the application.
Server Side:
The document http://support.microsoft.com/kb/301273 describes how
to create a simple Web service running on a Windows IIS-Server. We
have chosen to expose our collection of methods on the free host
www.somee.com.
Client Side:
We use the KSOAP 2.0 platform to request a sequence of remote
procedure calls to the IIS server hosting our service code. The methods
include functions taking zero, or more simple/object-complex inputs that
return simple/object-complex results.
http://www.java2s.com/Code/Jar/k/Downloadksoap2base254jar.htm
10 10 10
18C. Android - Internet WebServices
Consuming WebServices
Example 1 – TUTORIAL – IIS Server Side Code
10
Services Available at the IIS Server
[...]... (Content-Type, Set-Cookie, and others), and output data by writing to the response's output stream or output writer 30 18C Android - Internet WebServicesConsumingWebServices Example 2 – TUTORIAL – Android Code REST Protocol – Android & Apache Server 31 18C Android - Internet WebServicesConsumingWebServices Example 2 – TUTORIAL – Android Code REST Protocol – Android & Apache Server Reference:...18C Android - Internet Web ServicesConsumingWebServices Example 1 – TUTORIAL – IIS Server Side Code Android App accessing all services available at the IIS server 11 18C Android - Internet WebServicesConsumingWebServices Example 1 – TUTORIAL – Android Application Our Android app uses KSOAP 2 API KSOAP is a webservice client library for constrained Java... SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; 27 18C Android - Internet Web ServicesConsumingWebServices Example 1 – TUTORIAL – Android Code Android – Sending / Receiving Objects from WebServices ( 3 of 3) This fragment is from the Android s app WebServiceCall class It prepares the request object and its EnglishDistance object parameter, calls the service and picks the returned object // place in the... Internet Web ServicesConsumingWebServices Example 1 – TUTORIAL – Android Code Android – Sending / Receiving Objects from WebServices ( 2 of 3) This fragment is from the Android s app WebServiceCall class It prepares the request object and its EnglishDistance object parameter, calls the service and picks the returned object public EnglishDistance AddHalfFoot(EnglishDistance inputEd){ // indicate webservice... call webservice and parse returning response object EnglishDistance outputEd = null; final Object response = this.call( SOAP_ACTION + webMethod, envelope ); if (response != null) { outputEd = new EnglishDistance((SoapObject) response); } return outputEd; }//AddHalfFoot 28 29 18C Android - Internet Web ServicesConsumingWebServices Example 2 – TUTORIAL – Android Code REST Protocol – Android & Apache. .. (int)(value / 12); } this._inches = (value % 12); } } }//EnglishDistance } 25 18C Android - Internet Web ServicesConsumingWebServices Example 1 – TUTORIAL – Android Code Android – Sending / Receiving Objects from WebServices ( 1 of 3) This fragment is from the Android s app Main class It creates an instance of an EnglishDistance and sends it to the server to be increased by half a foot Transferring of... } 15 18C Android - Internet WebServicesConsumingWebServices Example 1 – TUTORIAL – Android Application Fragment of the WebServiceCall class called to add two numbers Here input parameters and output values resulting from the webservice are sent and received (cont 3/4) public int AddService(int v1, int v2){ int intResult = 0; // indicate webservice (endpoint) to be called final String webMethod... (TextView)findViewById(R.id.txtMsg); WebServiceCall webServiceCall = new WebServiceCall(); // add two numbers - get int result int intResult = webServiceCall.AddService(11, 22); txtMsg.append( "\nAdd RESULT= " + intResult ); } All the intelligent work is done by the WebServiceCall class 13 18C Android - Internet WebServicesConsumingWebServices Example 1 – TUTORIAL – Android Application The WebServiceCall class... object envelope.setOutputSoapObject(requestObject); 16 18C Android - Internet WebServicesConsumingWebServices Example 1 – TUTORIAL – Android Application The WebServiceCall class (continuation 4/4) Web service is called here try { // call webmethod and parse returning response object final Object response = (Object) this.call( SOAP_ACTION + webMethod, envelope); if (response != null) intResult = Integer.parseInt(response.toString());... interaction, it is strong-typed and supports synchronous, asynchronous, and complex-routing communication schemes Our implementation includes three classes 1 Main webcalls are assembled 2 EnglishDistance (Serialized) 3 WebServiceCall deals with HTTP transporting of the request/response and envelope objects 12 18C Android - Internet WebServicesConsumingWebServices Example 1 – TUTORIAL – Android Application