Android开发全程记录(十五)——android调用webservice的方法

1.android调用webservice接口需要使用第三方jar包ksoap2,KSoap2 Android 是Android平台上一个高效、轻量级的SOAP开发包。

2.Ksoap2-android jar包下载
      ksoap2 android当前的最新版本为2.5.4,名为ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar,它的下载地址是:http://code.google.com/p/ksoap2-android/,进入页面后,点击“Downloads”标签页,点击里面的链接下载。

3.下载后,将ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar放在项目的libs目录下,build path后就可以使用了。

4.具体调用

// 调用websevice接口,返回结果
						String nameSpace = "xxx";//命名空间,形式如:http://WebXml.com.cn/
						// 调用的方法名称
						String methodName = "xxxx";//形式如:getMobileCodeInfo
						// EndPoint
						String endPoint = "xxxxxx";//形式如:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
						// SOAP Action
						String soapAction = "xxxxxxxxxx";//形式如:http://WebXml.com.cn/getMobileCodeInfo

						// 指定WebService的命名空间和调用的方法名
						SoapObject rpc = new SoapObject(nameSpace, methodName);

						// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
						rpc.addProperty("params", "xxxx");

						// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
						SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
								SoapEnvelope.VER10);

						envelope.bodyOut = rpc;
						// 设置是否调用的是dotNet开发的WebService
						envelope.dotNet = true;
						// 等价于envelope.bodyOut = rpc;
						envelope.setOutputSoapObject(rpc);

						HttpTransportSE transport = new HttpTransportSE(
								endPoint);
						
						try {
// 调用WebService
transport.call(soapAction, envelope);
// 获取返回的数据
if (envelope.bodyIn != null) {
SoapObject object = (SoapObject) envelope.bodyIn;
if (object.getPropertyCount() > 0) {


jsonResult = object.getProperty(0).toString();
}
}
} catch (Exception e) { 
e.printStackTrace();
jsonResult="";
}

5.解释

当在浏览器中访问WSDL时,很容易得知命名空间、调用的方法名称是什么,至于EndPoint通常是将WSDL地址末尾的"?wsdl"去除后剩余的部分;而SOAP Action通常为命名空间 + 调用的方法名称。

6.补充一下,详细代码

/*
	 * 基本功能:调用webservice,返回json字符串
	 */
	public static String getWebserviceResult(String methodName,
			Map<String, String> map) {
		String nameSpace = Constants.NAMESPACE;
		String endPoint = Constants.ENDPOINT;
		String soapAction = nameSpace + methodName;
		// 指定WebService的命名空间和调用的方法名
		SoapObject rpc = new SoapObject(nameSpace, methodName);
		@SuppressWarnings("rawtypes")
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			@SuppressWarnings("rawtypes")
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();
			// 设置需调用WebService接口需要传入参数
			rpc.addProperty(key, val);
		}
		// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.bodyOut = rpc;
		// 设置是否调用的是dotNet开发的WebService
		envelope.dotNet = true;
		// 等价于envelope.bodyOut = rpc;
		envelope.setOutputSoapObject(rpc);
		String jsonResult = "";
		HttpTransportSE transport = new HttpTransportSE(endPoint, 3000);// 设置超时
		try {
			// 调用WebService
			transport.call(soapAction, envelope);
			// 获取返回的数据
			if (envelope.bodyIn != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				if (object.getPropertyCount() > 0) {

					jsonResult = object.getProperty(0).toString();
				}
			}
		} catch (Exception e) { 
			e.printStackTrace();
			jsonResult="";
		}
		return jsonResult;
	}


你可能感兴趣的:(android,webservice)