调用webservice总结:
1.加入第三方的jar包 Ksoap2-android-XXX
2.访问响应的webservice的网站,查看响应的信息,得到nameSpace,methodName,url,soapAction
3.如果request信息还有带有SoapHander的。那么就要封装:依据参数封装
Element[] header = new Element[1];
header[0] = new Element().createElement(nameSpace, "SoapHeader");
Element userName = new Element().createElement(nameSpace, "UserID");
userName.addChild(Node.TEXT, UserID);
header[0].addChild(Node.ELEMENT, userName);
Element pass = new Element().createElement(nameSpace, "PassWord");
pass.addChild(Node.TEXT, PassWord);
header[0].addChild(Node.ELEMENT, pass);
4.封装request信息的SoapBody
// 指定WebService的命名空间和调用的方法名
SoapObject soapObject=new SoapObject(nameSpace, methodName);
//处理soap12:Body数据部分soapObject.addProperty("loginName",username);soapObject.addProperty("password",password);
5.指定SoapSerializationEnvelope信息
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
//SoapEnvelope.VER11 表示使用的soap协议的版本号 1.1 或者是1.2
envelope.headerOut=header;
envelope.bodyOut=soapObject;
envelope.dotNet = true; //指定webservice的类型的(java,PHP,dotNet)
envelope.setOutputSoapObject(soapObject);
6.指定HttpTransportSE
HttpTransportSE ht = new HttpTransportSE(url);
7.访问webservice服务器
ht.call(soapAction, envelope);
8.两种方式获取服务器返回的信息
envelope.getResponse();
两者的区别:
Webservice开发时,我们接收webservice返回值可以使用
SoapObject soapObject = (SoapObject) envelope.getResponse();
但如果接受的webservice服务器端返回值是String类型的数值的话,这种方法往往会产生 java.lang.ClassCastException:
org.ksoap2.serialization.SoapPrimitive这样的错误。
如果服务器返回值的类型是string 或者 byte[] 的时候,可以使用
SoapObject result = (SoapObject)envelope.bodyIn;
Object object =envelope.getResponse();
但如果使用 base64进行解码和编码的时候只能使用前者,它可以完整的将byte[]进行解码和编码。
SoapObject result=(SoapObject) envelope.bodyIn;
String str=result.getProperty(0).toString();
byte[] ops = Base64.decode(str);
后者编程如下:
Object result= (Object) reqVo.envelope.getResponse();
String str=result.toString();
9.解析字符串str获取客户端想要的信息
我用的是pull解析器。
public static String[] parse(String result)// 把获取的result放进来解析
throws XmlPullParserException, IOException {
String[] strings = null;
String status = null;
String msg = null;
// 获得PULL解析器工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 指定由XmlPullParserFactory对象产生的解析器能提供对XML命名空间的支持
factory.setNamespaceAware(true);
// 获得XmlPullParser的实例
XmlPullParser xpp = factory.newPullParser();
// 设置需要解析的XML数据
xpp.setInput(new StringReader(result));
// 获得事件类型,开始解析
int eventType = xpp.getEventType();
// 若解析到末尾
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {// 文档开始
System.out.println("Start document");
}
if (eventType == XmlPullParser.START_TAG) {// 标签开始
if (xpp.getName().equals("Response")) {
strings = new String[2];
}
if (xpp.getName().equals("status")) {// 节点名称
status = xpp.nextText();
System.out.println("status==" + status);
}
if (xpp.getName().equals("msg")) {// 节点名称
msg = xpp.nextText();
System.out.println("msg==" + msg);
}
}
if (eventType == XmlPullParser.END_TAG) {// 标签结束
if (xpp.getName().equals("Response")) {
strings[0] = status;
strings[1] = msg;
}
}
eventType = xpp.next();// 下一标签
}
System.out.println("End document");
return strings;
}