地址如下:
https://github.com/loopj/android-async-http
选择android-async-http库来处理android网络通信处理,完全是因为呼声够高。期间在研究该库soap通信时,发现了关于该库做soap通信时的讨论帖,在网上资料很少的情况下,看到这个讨论也算是很好的教材。
https://github.com/loopj/android-async-http/issues/403
下来开始本地测试。
1.在本地搭建WebService服务,搭建方法: http://xushans.iteye.com/blog/2218801
WebService具体功能类:
package com.test; public class Axis2WB { /** * 提供了一个说Hello的服务 * * @return */ public String sayHello( String name ) { return "Hello " + name; } /** * 提供了一个做加法的服务 * * @param a * @param b * @return */ public int add( int a, int b ) { return a + b; } }
2.打包启动后,生成的wsdl:
This XML file does not appear to have any style information associated with it. The document tree is shown below. <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://test.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://test.com"> <wsdl:documentation>Axis2WB</wsdl:documentation> <wsdl:types> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://test.com"> <xs:element name="add"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="a" type="xs:int"/> <xs:element minOccurs="0" name="b" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="addResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="sayHello"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="sayHelloResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> <wsdl:message name="addRequest"> <wsdl:part name="parameters" element="ns:add"/> </wsdl:message> <wsdl:message name="addResponse"> <wsdl:part name="parameters" element="ns:addResponse"/> </wsdl:message> <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="ns:sayHello"/> </wsdl:message> <wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="ns:sayHelloResponse"/> </wsdl:message> <wsdl:portType name="Axis2WBPortType"> <wsdl:operation name="add"> <wsdl:input message="ns:addRequest" wsaw:Action="urn:add"/> <wsdl:output message="ns:addResponse" wsaw:Action="urn:addResponse"/> </wsdl:operation> <wsdl:operation name="sayHello"> <wsdl:input message="ns:sayHelloRequest" wsaw:Action="urn:sayHello"/> <wsdl:output message="ns:sayHelloResponse" wsaw:Action="urn:sayHelloResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="Axis2WBSoap11Binding" type="ns:Axis2WBPortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="add"> <soap:operation soapAction="urn:add" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="sayHello"> <soap:operation soapAction="urn:sayHello" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="Axis2WBSoap12Binding" type="ns:Axis2WBPortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="add"> <soap12:operation soapAction="urn:add" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="sayHello"> <soap12:operation soapAction="urn:sayHello" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="Axis2WBHttpBinding" type="ns:Axis2WBPortType"> <http:binding verb="POST"/> <wsdl:operation name="add"> <http:operation location="add"/> <wsdl:input> <mime:content type="application/xml" part="parameters"/> </wsdl:input> <wsdl:output> <mime:content type="application/xml" part="parameters"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="sayHello"> <http:operation location="sayHello"/> <wsdl:input> <mime:content type="application/xml" part="parameters"/> </wsdl:input> <wsdl:output> <mime:content type="application/xml" part="parameters"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="Axis2WB"> <wsdl:port name="Axis2WBHttpSoap11Endpoint" binding="ns:Axis2WBSoap11Binding"> <soap:address location="http://localhost:8080/axis2/services/Axis2WB.Axis2WBHttpSoap11Endpoint/"/> </wsdl:port> <wsdl:port name="Axis2WBHttpSoap12Endpoint" binding="ns:Axis2WBSoap12Binding"> <soap12:address location="http://localhost:8080/axis2/services/Axis2WB.Axis2WBHttpSoap12Endpoint/"/> </wsdl:port> <wsdl:port name="Axis2WBHttpEndpoint" binding="ns:Axis2WBHttpBinding"> <http:address location="http://localhost:8080/axis2/services/Axis2WB.Axis2WBHttpEndpoint/"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
3.使用SoapUI生成soap request:
http://xushans.iteye.com/blog/2220718
4.利用android-async-http编写代码:
String xmlRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:test=\"http://test.com\">" + "<soapenv:Header/>" + "<soapenv:Body>" + "<test:sayHello>" + "<test:name>liu</test:name>" + "</test:sayHello>" + "</soapenv:Body>" + "</soapenv:Envelope>"; String contentType = "text/xml;charset=utf-8"; StringEntity entity = null; try { entity = new StringEntity( xmlRequest, "UTF-8" ); } catch ( UnsupportedEncodingException e ) { // TODO Auto-generated catch block e.printStackTrace(); } entity.setContentType( contentType ); entity.setContentEncoding( new BasicHeader( HTTP.CONTENT_ENCODING, contentType ) ); AsyncHttpClient client = new AsyncHttpClient(); client.addHeader( "Content-Type", contentType ); client.addHeader( "SOAPAction", "http://test.com/:sayHello" ); client.post( context, "http://xxx.xxx.xxx.xxx:8080/axis2/services/Axis2WB?wsdl", entity, contentType, new AsyncHttpResponseHandler() { @Override public void onFailure( int arg0, Header[] arg1, byte[] arg2, Throwable arg3 ) { // TODO Auto-generated method stub String result = arg2.toString(); System.out.println( result ); } @Override public void onSuccess( int arg0, Header[] arg1, byte[] arg2 ) { // TODO Auto-generated method stub String result = new String( arg2 ); System.out.println( result ); } } );
其中,在xmlRequest中,设置需要调用webService的方法sayHello的参数 liu。创建异步通信对象AsyncHttpClient时,设置header属性 Content-Type为 text/xml;charset=utf-8,设置header属性 SOAPAction为 http://test.com/:sayHello(http://test.com是命名空间,sayHello是方法名)。设置AsyncHttpClient.post方法的url参数为 webService的wsdl的url。设置response控制类为异步类AsyncHttpResponseHandler。
测试结果,收到soap response:
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:sayHelloResponse xmlns:ns="http://test.com"><ns:return>Hello liu</ns:return></ns:sayHelloResponse></soapenv:Body></soapenv:Envelope>