首页 > 代码库 > Android_WebServices_源码分析

Android_WebServices_源码分析

本博文为子墨原创,转载请注明出处!
http://blog.csdn.net/zimo2013/article/details/38037989
在 Android_WebServices_介绍一文中,简单介绍了WebServices的基础知识,下面主要分析 ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar实现源码。

1.调用WebServices流程

public void getRemoteInfo(String phoneSec) {
	String nameSpace = "http://WebXml.com.cn/";
	String methodName = "getMobileCodeInfo";
	String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
	String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";

	// 1.初始化 SoapObject对象,为该方法设置参数,相当于信体
	SoapObject request = new SoapObject(nameSpace, methodName);
	request.addProperty("mobileCode", phoneSec);
	request.addProperty("userId", "");
	
	// 2.实例化SoapSerializationEnvelope对象,相当于信皮
	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
	envelope.bodyOut = request;
	envelope.dotNet = true;//兼容.net开发的Net-Services

	// 3.实例化HttpTransportSE对象,还可以指定放了访问的请求时间
	HttpTransportSE transport = new HttpTransportSE(endPoint);
	//HttpTransportSE transport = new HttpTransportSE(endPoint, timeout);
	try {
	// 4.核心方法调用,其中soapActon在SoapSerializationEnvelope.VER12时无效,且为POST请求
		transport.call(soapAction, envelope);
		
		SoapObject response = (SoapObject) envelope.bodyIn;
		final String result = response.getProperty(0).toString();//Vector
		toast(result);
	} catch (Exception e) {
		e.printStackTrace();
		toast(e.getMessage());
	}
}

2.transport.call关键源码分析

/**
 * Perform a soap call with a given namespace and the given envelope providing
 * any extra headers that the user requires such as cookies. Headers that are
 * returned by the web service will be returned to the caller in the form of a
 * <code>List</code> of <code>HeaderProperty</code> instances.
 *
 * @param soapAction
 *            the namespace with which to perform the call in.
 * @param envelope
 *            the envelope the contains the information for the call.
 * @param headers
 *   <code>List</code> of <code>HeaderProperty</code> headers to send with the SOAP request.
 * @param outputFile
 *              a file to stream the response into rather than parsing it, streaming happens when file is not null
 *
 * @return Headers returned by the web service as a <code>List</code> of
 * <code>HeaderProperty</code> instances.
 *
 * @throws HttpResponseException
 *              an IOException when Http response code is different from 200
 */
public List call(String soapAction, SoapEnvelope envelope, List headers, File outputFile)
    throws HttpResponseException, IOException, XmlPullParserException {

    if (soapAction == null) {
        soapAction = "\"\"";
    }

    //根据envelope,将其序列化为一个请求字节数组
    byte[] requestData = http://www.mamicode.com/createRequestData(envelope, "UTF-8");>

Android_WebServices_源码分析