Android中ksoap实现SOAP远程调用,支持字符串、数值、日期类型参数
综合网上的资料,将我开发调试通过的代码分享出来,好用就拿走、转走
如果在android中调用,请注意一定要在线程中执行!
-----------------------------------------------------------------
...
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
...
// 调用远程接口
public static String RunWebServices(String nameSpace, String endPoint,
String function, String type, String param, int timeout) throws Exception {
String soapAction = "";
soapAction = nameSpace + function; // SOAP Action
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
SoapObject rpc = new SoapObject(nameSpace, function);
// 根据参数类型及数据进行rpc数据设置
rpc = setProperty(rpc, type, param);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
// xuky 2015-05-19 添加超时时间参数
HttpTransportSE transport = new HttpTransportSE(endPoint,timeout*1000);
String result = "";
// 调用WebService
transport.call(soapAction, envelope);
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
result = object.getProperty(0).toString();
return result;
}
// 添加接口参数信息,目前支持三种类型:字符串、数值、日期
protected static SoapObject setProperty(SoapObject rpc, String type,
String param) {
int n = getSplitNum(param, "\\|");
if (n == 0) {
// 无参数
} else {
for (int i = 0; i < n; i++) {
String str = getSplitParam(type, "\\|", i);
if (str.indexOf("[int]") >= 0) {
String temp = getSplitParam(param, "\\|", i);
if (temp.equals(" ") || temp.equals("")){
str = getSplitParam(str, "\\[", 0);
rpc.addProperty(str, "");
}
else{
int num = Tool.String2Int(temp);
str = getSplitParam(str, "\\[", 0);
rpc.addProperty(str, num);
}
} else if (str.indexOf("[date]") >= 0) {
String temp = getSplitParam(param, "\\|", i);
if (temp.indexOf(" ") < 0) {
temp = temp + " 00:00:00";
}
Date oldTime = Tool
.string2Date(temp, "yyyy-MM-dd HH:mm:ss");
// 参考http://blog.csdn.net/tyjhfield/article/details/8705908
str = getSplitParam(str, "\\[", 0);
rpc.addProperty(str, Tool.getStrForWebSvr(oldTime));
} else {
rpc.addProperty(str, getSplitParam(param, "\\|", i));
}
}
}
return rpc;
}
-----------------------------------------------------------------
调用举例
//调用举例
String func = "GetList";
String str = "1290|150595903"
String param = "DjNo|DjID[int]"
String nameSpace = "http://example.com/";
String endPoint = "http://123.32.32.160:4134/Android/Test.asmx"
new Thread() {
public void run() {
String ret = RunWebServices(func, str,param );
}.start();
-----------------------------------------------------------------
远程接口位置(模拟数据,无法访问)
http://123.32.32.160:4134/Android/Test.asmx?op=GetList
接口返回数据
SOAP 1.1
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
POST /Android/Test.asmx HTTP/1.1
Host: 123.32.32.160
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://example.com/GetList"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetList xmlns="http://example.com/">
<DjNo>string</typeNo>
<DjID>int</typeNo>
</GetList>
</soap:Body>
</soap:Envelope>
-----------------------------------------------------------------
其他辅助程序
// 获得以某个符号分隔的多段数据中的一段数据
public static String getSplitParam(String param, String split, int index) {
if (param == null)
return null;
String[] array = param.split(split);
if (index >= array.length)
return "";
else {
if (array[index].equals(" "))
return "";
else
return array[index];
}
}
public static Date string2Date(String dateStr, String formatStr) {
DateFormat sdf = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}