Introduction to the SOAP learning notes for Android development

Source: Internet
Author: User
Tags soap object model xmlns wsdl

Simple Object Access Protocol (PROTOCOL,SOAP) is a standardized communication specification used primarily for Web services (Web service). The advent of SOAP enables the Web server to extract data from an XML database without having to take the time to format the page and allow different applications to exchange data with each other in an XML format, regardless of the programming language, platform, and hardware, through the HTTP protocol. This standard was jointly presented by IBM, Microsoft, Userland and DevelopMentor in 1998 and supported by companies such as IBM, Lotus (Lotus), Compaq (Compaq), and submitted to the World Wide Web Consortium in 2000 (Worldwide Wide Web consortium,w3c). SOAP version 1.1 is now the industry's common standard.

SOAP is based on the XML standard for sending messages in a distributed environment and performing remote procedure calls. With soap, you can serialize data without considering any particular transport protocol, although the HTTP protocol is typically used.

The advantages of soap are as follows:

SOAP is extensible. SOAP does not need to interrupt existing applications, and SOAP clients, servers, and protocols themselves evolve. And soap is a great support for intermediate media and hierarchical architecture.

Soap is simple. The client sends a request, invokes the corresponding object, and the server returns the result. These messages are in XML format and are encapsulated into messages that conform to the HTTP protocol. Therefore, it meets the requirements of any router, firewall, or proxy server.

Soap is completely unrelated to the manufacturer. SOAP can be implemented independently of the platform, the operating system, the target model, and the programming language. In addition, the parameter selection of transmission and language binding and data encoding is determined by the specific implementation.

SOAP is independent of programming languages. Soap can be done in any language, as long as the client sends the correct SOAP request (that is, passing an appropriate parameter to an actual remote server). SOAP has no object model, and applications can be bundled into any object model.

soap– message

SOAP uses the Internet Application layer protocol as its transport protocol. Both SMTP and the HTTP protocol can be used to transmit SOAP messages, and SOAP can also be transmitted over HTTPS. A SOAP message is an ordinary XML document that contains the following elements:

The required envelope element, which identifies the XML document as a SOAP message.

An optional header element that contains header information.

The required BODY element that contains all the invocation and response information.

An optional fault element that provides information about an error that occurred while processing this message.

The important syntax rules for SOAP messages are as follows:

SOAP messages must be encoded using XML.

SOAP messages must use the SOAP envelope namespace.

SOAP messages must use the SOAP encoding namespace.

A SOAP message cannot contain a DTD reference.

SOAP messages cannot contain XML processing directives.

Instance:

The message that is sent at request time is as follows:
<soapenv:envelope
xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd= "http://www.w3.org/2001/XMLSchema"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
<soapenv:body>
<req:echo xmlns:req= "http://localhost:8080/axis2/services/myser-vice/"
< Req:category>classifieds</req:category>
</req:echo>
</soapenv:body>
</soapenv : Envelope>

The message that is sent when responding is as follows:
<soapenv:envelope
xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/"
Xmlns:wsa= "http://schemas.xmlsoap.org/ws/2004/08/addressing" >
<soapenv:Header>
<wsa:replyto><wsa:address>http://schemas.xmlsoap.org/ws/2004/08/
Addressing/role/anonymous</wsa:address>
</wsa:ReplyTo>
<wsa:from><wsa:address>http://localhost:8080/axis2/
Services/myservice</wsa:address>
</wsa:From>
<wsa:MessageID>ECE5B3F187F29D28BC11433905662036</wsa:MessageID>
</soapenv:Header>
<soapenv:Body>
<req:echo xmlns:req= "http://localhost:8080/axis2/services/myservice/" >
<req:category>classifieds</req:category>
</req:echo>
</soapenv:Body>
</soapenv:Envelope>

soap– Call WebService

The specific steps of the SOAP invocation WebService are as follows.

Step 1 Add the KSOAP2 package. WebService is a remote calling standard based on SOAP protocol, which can integrate different operating system platform, different languages and different technologies into one webservice. There is no library to invoke WebService in the Android SDK, so you need to use a Third-party SDK to invoke WebService. The PC version of the WebService client library is very rich, such as Axis2, CXF, and so on, but these development packages are too large for the Android system and may not be easily ported to Android. Therefore, these development packages are not within our consideration. Suitable for mobile phone WebService Client SDK Some, more commonly used is KSOAP2, can download from the URL Http://code.google.com/p/ksoap2-android/, The downloaded Ksoap2-android-assembly-2.4-jar-with-dependencies.jar package is then copied to the Lib directory of the Eclipse Project and, of course, can be placed in other directories. Reference this jar package in the Eclipse project.

Step 2 Specifies the namespace of the WebService and the method name of the call, such as:
Soapobject request =new Soapobject (http://service, "GetName");

The first parameter of the Soapobject class represents the namespace of the webservice, the namespace of the webservice can be found from the WSDL document, and the second parameter represents the WebService method name to invoke.

Step 3 Sets the parameter value of the calling method, which can be omitted if there are no arguments. The code for setting the parameter value of the method is as follows:
Request.addproperty ("Param1″", "value");
Request.addproperty ("Param2″", "value");

Note that the 1th parameter of the AddProperty method, while representing the parameter name of the calling method, does not necessarily match the method parameter name in the WebService class on the server side, as long as the order of the parameters is set.

Step 4 generates the SOAP request information that invokes the WebService method. This information is described by the Soapserializationenvelope object, and the code is as follows:
Soapserializationenvelope envelope=
New Soapserializationenvelope (SOAPENVELOPE.VER11);
Envelope.bodyout = Request;

When creating a Soapserializationenvelope object, you need to set the version number of the SOAP protocol through the Soapserializationenvelope class's construction method. The version number needs to be set according to the WebService version number of the service side. After you create the Soapserializationenvelope object, do not forget to set the Bodyout property of the Soapsoapserializationenvelope class, which is the Soapobject object you created in step 2.

Step 5 Create the Httptransportsse object. You can specify the URL of a WebService WSDL document by using the Httptransportsse class's construction method.
Httptransportse ht=new Httptransportse
("http://fy.webxml.com.cn/webservices/englishchinese.asmxwsdl");

Step 6 invokes the WebService method using the Call method, as follows:
Ht.call (Null,envelope);

The first parameter of the call method is generally null, and the 2nd parameter is the Soapserialization-envelope object created in step 4.

Step 7 uses the GetResponse method to obtain the return result of the WebService method, as follows:
Soapobject Soapobject = (soapobject) envelope.getresponse ();

Step 8 resolves the returned content.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.