`

使用android-async-http发送soap异步请求

阅读更多
android-async-http是处理http处理的开源网络框架。
地址如下:
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-Typetext/xml;charset=utf-8,设置header属性SOAPActionhttp://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>
分享到:
评论

相关推荐

    安卓网站交互JSONxmlWebserviceUPnP相关-android-async-http是Android上的一个异步基于回调的HTTP客户端开发包建立在Apache的HttpClient库上.zip

    android-async-http是Android上的一个异步、基于回调的HTTP客户端开发包,建立在Apache的HttpClient库上.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    android-async-http 源码

    https://oss.sonatype.org/content/repositories/snapshots/com/loopj/android/android-async-http/ Maven URL: https://oss.sonatype.org/content/repositories/snapshots/ GroupId: com.loopj.android ArtifactId...

    android-async-http-1.4.5.jar

    最新异步网络请求android-async-http-1.4.5.jar

    android-async-http-1.4.3最新jar包

    android的异步网络加载,方便使用,免去多余的代码

    android-async-http官方项目

    android-async-http官方项目:来自Github; 该项目中包含了多个版本的android-async-http.jar包,目前最新版本是:android-async-http-1.4.5.jar; 地址:原项目地址:https://github.com/loopj/android-async-http

    Android-async-http-1.4.9.jar

    android-async-http-1.4.9.jar是一般使用Apache HTTP Client或者采用HttpURLConnect,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使用android-async-http这个库可以大大的简化操作,它是...

    android-async-http-1.4.9下载

    android-async-http 1.4.9版本,附带了其依赖的httpclient包

    android-async-http

    android-async-http开源框架可以是我们轻松的获取网络数据或者向服务器发送数据,使用起来也很简单,具体详细使用看官网:https://github.com/loopj/android-async-http

    android-async-http-master.zip

    2017最新版本android-async-http-master Android异步框架,并附有简单的导入方法以及报错处理

    android-async-http-1.4.11.zip

    1)包里包含android-async-http-1.4.11.jar 和 httpclient-4.5.8.jar两个文件 2)强大的网络请求库,主要特征如下: 处理异步Http请求,并通过匿名内部类处理回调结果 Http请求均位于非UI线程,不会阻塞UI操作 通过...

    android-async-http-1.4.8.jar

    强大的网络请求库,...处理异步Http请求,并通过匿名内部类处理回调结果 Http请求均位于非UI线程,不会阻塞UI操作 通过线程池处理并发请求 处理文件上传、下载 响应结果自动打包JSON格式 自动处理连接断开时请求重连

    android-async-http的jar包

    android.async.http.jar包 包含1.2.0 至 1.4.8 版本。

    android-async-http-1.4.6

    android-async-http是专门针对Android在Apache的HttpClient基础上构建的异步http连接。所有的请求全在UI(主)线程之外执行,而callback使用了Android的Handler发送消息机制在创建它的线程中执行。

    android-async-http-1.4.9

    使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息

    android-async-http-1.4.6.jar

    比较好用的安卓异步下载包,android-async-http-1.4.6.jar

    android-async-http-1.4.4

    使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息

    android-async-http.jar

    android-async-http-1.4.8.jar提供下载学习和使用,其中还有别的版本可以使用

    android-async-http-1.4.3.jar 包

    安卓开发中经常使用到的一个框架,使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message ...

    android-async-http-master

    android-async-http开源框架可以是我们轻松的获取网络数据或者向服务器发送数据,使用起来也很简单

    android-async-http访问webservice

    网上的ksoap2访问webservice得自己写线程,线程自己控制有点问题,所以自己组装一下开源android-async-http,可以访问webservice.使用时候需要一定的Android开发经验才能使用

Global site tag (gtag.js) - Google Analytics