Android通过ksoap调用webservice


所需要ksoap jar下载: http://download.csdn.net/detail/w123456h19/4676150


import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


/**
 * 调用WebService
 * @author kewh 2012-07-24
 */
public class WebServiceHelper {
   
// 非.net提供的WebService
private Boolean dotNet = Boolean.FALSE;
// 请求响应时间(毫秒)
private Integer timeout = 60000;

public String callWebService(String methodName,Map<String, String> paramMap){
String result = null;
if(StringUtils.isNotBlank(methodName)){
String[] strs = callWebServices(methodName,paramMap);
if(null!=strs && strs.length>0){
result = strs[0];
}
}
return result;
}

public String[] callWebServices(String methodName,Map<String, String> paramMap){
String[] result = null;
try {
SoapObject soapObject = initSoapObject(methodName,paramMap);
if(null != soapObject && soapObject.getPropertyCount() > 0){
result = new String[soapObject.getPropertyCount()];
for(int i=0;i< soapObject.getPropertyCount();i++){
   SoapPrimitive value=(SoapPrimitive) soapObject.getProperty(i);
   result[i] = value.toString();
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return result;
}


private SoapObject initSoapObject(String methodName , Map<String, String> paramMap) throws Exception {
String nameSpace = LoadProperties.getInstance().getPropertieValue("service_namespace");
String serviceAction = LoadProperties.getInstance().getPropertieValue("service_action");

SoapObject request = new SoapObject(nameSpace, methodName);
PropertyInfo propertyInfo  = getPropertyInfo(paramMap);
if( null != propertyInfo ){
request.addProperty(propertyInfo);
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER10 | SoapEnvelope.VER11 );
envelope.bodyOut = request;
envelope.dotNet = getDotNet();
envelope.setOutputSoapObject(request);

HttpTransportSE transport = new HttpTransportSE(serviceAction, getTimeout());
transport.debug = true;
transport.call( nameSpace + methodName , envelope);
SoapObject soapObject = (SoapObject) envelope.bodyIn;
return soapObject;
}

private PropertyInfo getPropertyInfo(Map<String, String> paramMap) throws Exception{
PropertyInfo propertyInfo = null;
if(null!=paramMap && paramMap.size()>0){
propertyInfo = new PropertyInfo();
for(Entry<String, String> entry : paramMap.entrySet()){
propertyInfo.setName(entry.getKey());
propertyInfo.setValue(entry.getValue());
propertyInfo.setType(String.class);
}
}
return propertyInfo;
}

public Boolean getDotNet() {
return dotNet;
}


/** 设置 .net提供的WebService */
public void setDotNet(Boolean dotNet) {
this.dotNet = dotNet;
}

/** 请求响应时间(毫秒) */
public Integer getTimeout() {
return timeout;
}

/** 设置请求响应时间(毫秒) */
public void setTimeout(Integer timeout) {
this.timeout = timeout;

}

}



/**
 * 读取属性文件
 * @author kewh 2012-07-24
 */
public class LoadProperties {

    private static LoadProperties loadPropertie  = null;

    /** 获取实例 */
public static synchronized LoadProperties getInstance() {
if (loadPropertie == null) {
loadPropertie = new LoadProperties();
}
return loadPropertie;
}

private Properties getPropertie(){
Properties config = new Properties();
try {
InputStream inStream = getClass().getClassLoader().getResourceAsStream("resources/service_address.properties");
config.load(inStream);
} catch (Exception e) {
e.printStackTrace();
}
return config;
}

    private Properties getPropertie(String address) {
Properties config = new Properties();
try {
InputStream inStream = getClass().getClassLoader().getResourceAsStream(address);
config.load(inStream);
} catch (Exception e) {
e.printStackTrace();
}
return config;
}

    public  String getPropertieValue(String key) {
String result = null;
Properties config  = getInstance().getPropertie();
if (config != null) {
result = config.getProperty(key);
}
return result;
}
    
public  String getPropertieValue(String address,String key) {
String result = null;
Properties config  = getInstance().getPropertie(address);
if (config != null) {
result = config.getProperty(key);
}
return result;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值