您的位置:首页 > 其它

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐