package com.example.common; |
import java.util.HashMap; |
import java.util.Iterator; |
import java.util.Map; |
import java.util.Map.Entry; |
import java.util.Set; |
import org.ksoap2.SoapEnvelope; |
import org.ksoap2.serialization.SoapObject; |
import org.ksoap2.serialization.SoapSerializationEnvelope; |
import org.ksoap2.transport.HttpTransportSE; |
import android.util.Log; |
public class MyWebService { |
/** |
* 调用WebService |
* |
* @param nameSpace |
* 命名空间 |
* @param methodName |
* 调用的方法名称 |
* @param url |
* 地址 |
* @param soapAction |
* 命名空间和方法名 |
* @param map |
* 参数【键值对传值】 |
* @return |
*/ |
public SoapObject getWebServiceInfo(String nameSpace, String methodName, |
String url, String soapAction, HashMap<String, Object> map) throws Exception { |
// 指定WebService的命名空间和调用的方法名 |
SoapObject rpc = new SoapObject(nameSpace, methodName); // xxxxxxxxxxxx |
// 设置需调用WebService接口需要传入的参数(通过hashmap遍历) |
Set<Entry<String, Object>> set = map.entrySet(); |
Iterator<Entry<String, Object>> iterator2 = set.iterator(); |
while (iterator2.hasNext()) { |
Map.Entry<String, Object> entry = iterator2.next(); |
rpc.addProperty(entry.getKey(), entry.getValue()); |
} |
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本 |
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( |
SoapEnvelope.VER11); |
envelope.bodyOut = rpc; |
// 设置是否调用的是dotNet开发的WebService |
envelope.dotNet = true ; |
// 等价于envelope.bodyOut = rpc; |
envelope.setOutputSoapObject(rpc); |
HttpTransportSE transport = new HttpTransportSE(url, 30000 ); // xxxxxxxxxxxxxxxx |
transport.call(soapAction, envelope); // xxxxxxxxxx |
// 获取返回的数据 |
SoapObject object = (SoapObject) envelope.bodyIn; |
return object; |
} |
} |