ksoap调用wcf服务并传递类参数

首先该类需要继承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;
    	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值