使用soap协议(含请求头)访问webservice

什么是webService呢?,它是一种基于SAOP协议的远程调用标准,webservice可以将不同操作系统平台,不同语言,不同技术整合到一起。我们在PC机器java客户端中,需要一些库,比如XFire,Axis2,CXF等等来支持访问WebService,但是这些库并不适合我们资源有限的android手机客户端,做过JAVA ME的人都知道有KSOAP这个第三方的类库,可以帮助我们获取服务器端webService调用,当然KSOAP已经提供了基于android版本的jar包了,那么我们就开始吧:

首先下载KSOAP包:ksoap2-android-assembly-3.0.0-jar-with-dependencies

注意:使用3.5.0的jar包时,间隔一段时间访问时,第一次总是出错,建议使用3.0
的可避免此情况

主要针对的是:含有请求头时,去除body标签里的id、root属性,子标签的i:type属性

工具类:

import java.io.IOException;

import java.net.InetSocketAddress;

import java.util.List;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import org.apache.http.client.HttpResponseException;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.PropertyInfo;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import org.kxml2.kdom.Element;

import org.kxml2.kdom.Node;

import org.xmlpull.v1.XmlPullParserException;

import android.net.Proxy;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import com.newcapec.utils.AppConfig;

import com.newcapec.utils.WebServiceUtil11.WebServiceCall;

public class GetMoneyRequestWebService {

private static final ExecutorService executorService = Executors.newFixedThreadPool(3);

//命名空间

private static final String NAMESPACE =AppConfig.HWWebServiceNameSpace;

/**
    *访问领款申请WebService
 * @param methodName
 * WebService的调用方法名
 * @param properties
 *  WebService的参数
 * @param webServiceCallBack
 *  回调接口
 */

public static void callWebService(String methodName,String customercode,String posCode,

final WebServiceCallRequest webServiceCallBack) {

//创建HttpTransportSE对象,传递WebService服务器地址
final HttpTransportSE httpTransportSE = new HttpTransportSE(AppConfig.HuaWeiGetMoneyWebService);

Element[] header = new Element[1];
header[0] = new Element().createElement(NAMESPACE,"CredentialSoapHeader");

Element appID = new Element().createElement(NAMESPACE, "AppID");
appID.addChild(Node.TEXT, "1");
header[0].addChild(Node.ELEMENT, appID);

Element appTypeID = new Element().createElement(NAMESPACE, "AppTypeID");
appTypeID.addChild(Node.TEXT, "1");
header[0].addChild(Node.ELEMENT, appTypeID);

Element iP = new Element().createElement(NAMESPACE, "IP");
iP.addChild(Node.TEXT, "192.168.50.212");
header[0].addChild(Node.ELEMENT, iP);

Element timestamp = new Element().createElement(NAMESPACE, "Timestamp");
timestamp.addChild(Node.TEXT, "20160725144600");
header[0].addChild(Node.ELEMENT, timestamp);

Element random = new Element().createElement(NAMESPACE, "Random");
random.addChild(Node.TEXT, "33");
header[0].addChild(Node.ELEMENT, random);

Element hash = new Element().createElement(NAMESPACE, "Hash");
hash.addChild(Node.TEXT, "d3FlcmU=");
header[0].addChild(Node.ELEMENT, hash);

Element ver = new Element().createElement(NAMESPACE, "Ver");
ver.addChild(Node.TEXT, "1.0");
header[0].addChild(Node.ELEMENT, ver);

//创建SoapObject对象

SoapObject soapObject = new SoapObject(NAMESPACE, methodName);

                PropertyInfo info = new PropertyInfo();
                info.setName("customercode");
        info.setNamespace(NAMESPACE);
        info.setValue(customercode);
        info.setType(String.class);
    
        PropertyInfo info1 = new PropertyInfo();
        info1.setName("posCode");
        info1.setNamespace(NAMESPACE);
        info1.setValue(posCode);
        info1.setType(String.class);
                
        soapObject.addProperty(info);
        soapObject.addProperty(info1);

//实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号
final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER11);

//设置是否调用的是.Net开发的WebService
soapEnvelope.headerOut=header;
soapEnvelope.bodyOut=soapObject;
soapEnvelope.implicitTypes = true;//去除子标签的i:type属性
soapEnvelope.dotNet = false;
httpTransportSE.debug = true;
soapEnvelope.setAddAdornments(false);//去除body标签里的id、root属性

    //用于子线程与主线程通信的Handler
    final Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            //将返回值回调到callBack的参数中

            webServiceCallBack.callBack((SoapObject) msg.obj);

        }

    };

    //开启线程去访问WebService

    executorService.submit(new Runnable() {

            public void run() {

            SoapObject resultSoapObject = null;

            try {

                httpTransportSE.call(null, soapEnvelope);

                if (soapEnvelope.getResponse() != null) {

                    //获取服务器响应返回的SoapObject

                    resultSoapObject = (SoapObject) soapEnvelope.bodyIn;

                }

            } 

(Exception e) {

                e.printStackTrace();

            }finally {

                //将获取的消息利用Handler发送到主线程

                mHandler.sendMessage(mHandler.obtainMessage(0,

                        resultSoapObject));

            }

        }

    });

}

public interface WebServiceCallRequest {

    public void callBack(SoapObject result);

}

}

调用的代码:

GetMoneyRequestWebService.callWebService("Request_Electr_Cash","

传入的参数
",new WebServiceCallRequest() {

public void callBack(SoapObject soapObject) {

if (soapObject != null) {

String result = soapObject.getProperty(0).toString();

String out_result = soapObject.getProperty("out_result").toString();

String out_msg = soapObject.getProperty("out_msg").toString();

if (out_result.equals("00000")) {

请求成功的操作
} });

http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html
可以参考

你可能感兴趣的:(使用soap协议(含请求头)访问webservice)