android 连接webservice (SoapObject )

private static final String TAG = " ";
/** WEBSERVICE访问地址 */
private static final String URL = "http  ....";
/** WEBSERVICE 命名空间 */

private static final String URL_SPACE = "http  ....";


/** 方式一:webservice传过来的一个json数据格式*/


/**

* @param mehtodName
*            调用的方法名
* @param map
*            需要传入的参数
* @return
*/
@SuppressWarnings("rawtypes")
public String query(String mehtodName, Map<String, String> map) {
System.out.println("webservice query()被调用!");
String result = "";
try {
SoapObject soapObject = new SoapObject(URL_SPACE, mehtodName);
Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
@SuppressWarnings("unchecked")
Map.Entry<String, String> entry = (Entry<String, String>) it
.next();
soapObject.addProperty(entry.getKey(), entry.getValue());
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.bodyOut = soapObject;
envelope.dotNet = false;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE transportSE = new HttpTransportSE(URL);
transportSE.call(URL_SPACE + mehtodName, envelope);
if(envelope.getResponse()!=null){
SoapObject object = (SoapObject) envelope.bodyIn;
result = object.getProperty(0).toString();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("webservice query()被调用!异常");
result = "false";
}
System.out.println("result********"+result);
return result;
}


/** 方式:webservice传过来的一个list或者是单一的数据*/


/**
 * 
 * @param mehtodName
 *            调用的方法名
 * @param tabname
 *            需要传入的参数
 * @return
 */

public List<Object> query(String mehtodName, String tabname) {

List<Object> result = new ArrayList<Object>();

try {
SoapObject soapObject = new SoapObject(URL_SPACE, mehtodName);

soapObject.addProperty("val", tabname);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = soapObject;
envelope.dotNet = false;
envelope.setOutputSoapObject(soapObject);

HttpTransportSE transportSE = new HttpTransportSE(URL);

transportSE.call(URL_SPACE + mehtodName, envelope);
SoapObject object = (SoapObject) envelope.getResponse();

             //如果获取的是个集合,就对它进行下面的操作
             if(object.getName().equals("anyType")){
                 //遍历Web Service获得的集合
                 for(int i=0;i<object.getPropertyCount();i++){
                Object m = object.getProperty(i);
                    result.add(m);
                 }
             }
} catch (Exception e) {
e.printStackTrace();
return null;
}
return result;
}




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值