服务器端Webservice接口参数使用的是自定义结构体,非通用的一些字符串类。
Android 客户端使用ksoap2 jar包,使用方法如下:
//调用的方法
String methodName = "sceneUserValidate";
//创建httpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);//service url
ht.debug = true;
//使用soap1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//实例化SoapObject对象
SoapObject request = new SoapObject(SERVICE_NS, methodName);
/**
* 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
* */
SceneUserValidateEvt sevt = new SceneUserValidateEvt();
sevt.setCallNumber("13913008213");
sevt.setInaccessInfo(new InaccessInfo("0020001","0020001","12","12","v1.0"));
request.addProperty("SceneUserValidateEvt", sevt);
//将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
Log.d("haha","start = ");
try{
//调用webService
ht.call(SERVICE_NS+methodName, envelope);
Log.d("haha","dump = "+ht.requestDump);
//txt1.setText("看看"+envelope.getResponse());
if(envelope.getResponse() != null){
SoapObject result = (SoapObject) envelope.bodyIn;
String name = result.getProperty(0).toString();
Log.d("haha","返回值 = "+name);
}else{
Log.d("haha","返回值 =! ");
}
}catch (Exception e) {
e.printStackTrace();
}
04-12 21:31:12.056: W/System.err(4202): at java.lang.Thread.run(Thread.java:856)
第一反应将SceneUserValidateEvt结构体implements Serializable,可是运行后还是如上提示错误。
goolge串行化失败的大多数场景与我的不符合,网上比较多的是一些Integer.class 串行失败的场景,不过后来想了想翻了一下ksoap的包
有一个这个接口类org.ksoap2.serialization.KvmSerializable;
尝试了一下将SceneUserValidateEvt结构体implements KvmSerializable,并实现了以下4个接口,运行成功OK。
public abstract interface org.ksoap2.serialization.KvmSerializable { // Method descriptor #4 (I)Ljava/lang/Object; public abstract java.lang.Object getProperty(int arg0); // Method descriptor #6 ()I public abstract int getPropertyCount(); // Method descriptor #8 (ILjava/lang/Object;)V public abstract void setProperty(int arg0, java.lang.Object arg1); // Method descriptor #10 (ILjava/util/Hashtable;Lorg/ksoap2/serialization/PropertyInfo;)V public abstract void getPropertyInfo(int arg0, java.util.Hashtable arg1, org.ksoap2.serialization.PropertyInfo arg2); }