ksoap调用wcf服务并传递类参数
2014-03-19 13:50
225 查看
首先该类需要继承KvmSerializable 接口,并实现接口的getProperty,getPropertyCount,getPropertyInfo和setPropertyInfo4个方法,在发送数据前,序列化时,会回调这些方法。
// 类的具体实现 package com.test; import java.util.Hashtable; import org.ksoap2.serialization.KvmSerializable; import org.ksoap2.serialization.PropertyInfo; public class SimpleStu implements KvmSerializable { public int ID; public String Name; @Override public Object getProperty(int arg0) { switch (arg0){ case 0: return ID; case 1: return Name; default: return null; } } @Override public int getPropertyCount() { return 2; } @Override public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) { switch (arg0){ case 0:{ arg2.type = PropertyInfo.INTEGER_CLASS; arg2.name = "ID"; } case 1:{ arg2.type = PropertyInfo.STRING_CLASS; arg2.name = "Name"; } } } @Override public void setProperty(int arg0, Object arg1) { switch (arg0){ case 0:{ ID = Integer.parseInt(arg1.toString()); } case 1:{ Name = arg1.toString(); } } } public PropertyInfo getPropertyInfo(String name){ PropertyInfo ret = new PropertyInfo(); ret.setName(name); ret.setValue(this); ret.setType(SimpleStu.class); return ret; } public static SimpleStu create(int id, String name){ SimpleStu ret = new SimpleStu(); ret.setProperty(0, id); ret.setProperty(1, name); return ret; } } // 调用方法 package com.test; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; public class TestServiceHelper { private static final String NameSpace = "http://tempuri.org/"; private static final String Url = "http://192.168.0.100/test/test.svc"; public static SoapObject call(String NameSpace, String MethodName, String Url, String Action, List<PropertyInfo> Propertys){ SoapObject soapObject = new SoapObject(NameSpace, MethodName); for(PropertyInfo arg : Propertys){ soapObject.addProperty(arg); } HttpTransportSE trans = new HttpTransportSE(Url); trans.debug = true; // 使用调试功能 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // 版本 envelope.dotNet = true; envelope.bodyOut = trans; envelope.setOutputSoapObject(soapObject); // 映射 envelope.addMapping(NameSpace, "SimpleStu", SimpleStu.class); try { trans.call(Action, envelope); System.out.println("Call Successful!"); } catch (IOException e) { System.out.println("IOException"); e.printStackTrace(); } catch (XmlPullParserException e) { System.out.println("XmlPullParserException"); e.printStackTrace(); } catch(Exception e){ System.out.println(e.getMessage()); } return (SoapObject)envelope.bodyIn; } public static String addStudent(){ String methodName = "AddStudent"; String action = NameSpace + methodName; List<PropertyInfo> args = new ArrayList<PropertyInfo>(); args.add(SimpleStu.create(0, "0123").getPropertyInfo("stu")); try{ SoapObject ret = call(NameSpace, methodName, Url, action, args); return ret.getProperty(0).toString(); } catch(Exception ex){ return null; } }
相关文章推荐
- 实现在GET请求下调用WCF服务时传递对象(复合类型)参数
- jQuery调用WCF服务时如何传递对象参数
- 实现在GET请求下调用WCF服务时传递对象(复合类型)参数
- jQuery调用WCF服务时如何传递对象参数
- IOS调用WCF提供的服务方法,但是方法的参数是WCF那边自定义的对象,这样有办法调用么,如果可以IOS应该怎么传参呢?请问有了解的么,
- [译]jQuery调用WCF服务传递JSON对象
- jQuery调用WCF服务传递JSON对象
- delphi调用C# WEB服务,参数传递问题 (转)
- jQuery调用WCF服务传递JSON对象
- ksoap调用cxf webservice传递类对象参数,返回类对象
- WCF服务——jQuery调用传递JSON对象、XML对象
- C#调用Java端Web服务(CXF)时发生Integer参数类型无法传递的问题
- jQuery调用WCF服务传递JSON对象
- xfire客户端调用cxf服务的时候 传递过去的参数,String 类型的 老是为null
- 可以使用可用的服务和参数调用在“eWorld.WCFImplement.ServiceImplement.ImageArchiveService”类型上使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到的构造函数: 无法解析参数'eWorld.WCFBLL.ImageArchive.IDocumentOperation
- Python通过suds调用WCF服务 传递Python List 到 WCF Service
- 在Android中调用KSOAP2库访问webservice服务出现的服务端传入参数为null的问题解决
- PHP通过传递对象参数调用asp.net Webservice 服务
- ios--网页js调用oc代码+传递参数+避免中文参数乱码的解决方案(实例)
- WCF RESTful 服务+Jquery 客户端 跨域调用 大坑及解决方案汇总