ksoap2-android 调用webservice 示例代码,及注意事项

/**
* 校验密码
*/
public static UserInfo verify(String account, String password,
String methodName) {
UserInfo user = new UserInfo();
String soapAction = nameSpace + methodName;
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 设置调用WebService接口需要传入的参数
rpc.addProperty("usercode", account);
rpc.addProperty("md5Password", MD5.getMD5(password));
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);


envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endPoint, 30000);
transport.debug = true;// 是否是调试模式
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (IOException e) {
user.setCanLogin(false);
user.setErrInfo(e.getMessage());
e.printStackTrace();
} catch (XmlPullParserException e) {
user.setCanLogin(false);
user.setErrInfo(e.getMessage());
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
if (object != null) {
String result = object.getProperty(0).toString();


try {
JSONObject jo = new JSONObject(result);
int status = jo.getInt("status");


if (status == 0) {
user.setUserCode(jo.getString("usercode"));
user.setUserName(jo.getString("username"));
user.setToken(jo.getString("token"));
user.setUserId(jo.getLong("userid"));
user.setCanLogin(true);
} else {
user.setCanLogin(false);
user.setErrInfo(jo.getString("errInfo"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return user;

}

注意事项:1.soapAction 在后台给的wsdl中可以看到,但有时是空的,比如这样:所以要知道,soapAction = nameSpace+methodName;

    2.设置调用WebService接口需要传入的参数,这些参数的赋值顺序必须与webservice方法的参数顺序一致,并且参数的类型也要一致。

    3.endPoint在wsdl中最下边的位置可找到:<wsdlsoap:address location="http://***************/services/****e"/>

    4.必须自行开启子线程。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值