ksoap2实现webservice工具类


package com.ksoaptest.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

/**
* webservice处理类
* @author raymon
*
*/
public class WebServiceUtil {
// 定义Web Service的命名空间
static final String SERVICE_NAMESPACE = "http://192.168.20.159:8080/services/TestService";
// 定义Web Service提供服务的URL
static final String SERVICE_URL = "http://192.168.20.159:8080/services/TestService";

/**
* 发送webservice请求,并返回特定对象链表
* @param method 方法名
* @param paramNames 参数列表
* @param paramValues 参数值列表
* @param responseClass 返回类
* @return
*/
public static List<Object> callService(String method, String [] paramNames, String [] paramValues, Class responseClass, boolean isArray){
// 创建HttpTransportSE传输对象
HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
httpTranstation.debug = true;

// 使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE, method);

// 设置webservice方法参数值
for (int i = 0; i < paramNames.length; i++) {
soapObject.addProperty(paramNames[i], paramValues[i]);
}

envelope.bodyOut = soapObject;
try
{
// 调用Web Service
httpTranstation.call(null, envelope);
if (envelope.getResponse() != null)
{
// 获取服务器响应返回的SOAP消息
SoapObject bodyIn = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) bodyIn.getProperty(method + "Return");
return parseResponse(detail, responseClass, method, isArray);
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}

/**
* 处理webservice回复,并返回对象列表
* @param result
* @param classes
* @param method
* @return List<Object>
* @throws InstantiationException
* @throws IllegalAccessException
*/
private static List<Object> parseResponse(SoapObject result, Class classes,String method, boolean isArray)
throws InstantiationException,
IllegalAccessException {
List<Object> results = new ArrayList<Object>();

if (isArray) {
for (int i = 0; i < result.getPropertyCount(); i++) {
Object instance = classes.newInstance();
Field[] fields = classes.getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
String value = ((SoapObject) result.getProperty(i)).getProperty(fields[j].getName()).toString();
setPropValue(instance, fields[j].getName(), value);
}
results.add(instance);
}
} else {
Object instance = classes.newInstance();
Field[] fields = classes.getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
String value = result.getProperty(fields[j].getName()).toString();
setPropValue(instance, fields[j].getName(), value);
}
results.add(instance);
}
return results;
}

/**
* 反射设置对象的特定属性的值
* @param targetObj
* @param propName
* @param propValue
*/
public static void setPropValue(Object targetObj, String propName, Object propValue) {
Class targetClass = targetObj.getClass();
try {
Class targetC = Class.forName(targetClass.getName());
Field field = targetC.getDeclaredField(propName);
field.setAccessible(true);
if (field.getType().equals(Integer.class)) {
field.set(targetObj,
new Integer(Integer.valueOf(propValue.toString())));
}
if (field.getType().equals(int.class)) {
field.setInt(targetObj, Integer.valueOf(propValue.toString()));
}
if (field.getType().equals(String.class)) {
field.set(targetObj, propValue.toString());
}
if (field.getType().equals(double.class)) {
field.setDouble(targetObj, Double.valueOf(propValue.toString()));
}
if (field.getType().equals(Double.class)) {
field.set(targetObj,
new Double(Double.valueOf(propValue.toString())));
}
if (field.getType().equals(float.class)) {
field.setFloat(targetObj, Float.valueOf(propValue.toString()));
}
if (field.getType().equals(Float.class)) {
field.set(targetObj,
new Float(Float.valueOf(propValue.toString())));
}
if (field.getType().equals(Long.class)) {
field.set(targetObj,
new Long(Long.valueOf(propValue.toString())));
}
if (field.getType().equals(long.class)) {
field.setFloat(targetObj, Long.valueOf(propValue.toString()));
}
if (field.getType().equals(short.class)) {
field.setShort(targetObj, Short.valueOf(propValue.toString()));
}
if (field.getType().equals(Short.class)) {
field.set(targetObj,
new Short(Short.valueOf(propValue.toString())));
}
if (field.getType().equals(java.util.Date.class)) {
field.set(targetObj, new Date(Date.parse(propValue.toString())));
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
//转载请注明出处:http://forhope.iteye.com/blog/1461407
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值