首先要使用soap與服務器通信,需要下載KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包(或者版本更高的)。
接着,先整體感受下如何調用服務的:
public String getTaskSoap(String userCode, String taskType, String userType) {
// 命名空間
String nameSpace = "vaecer";
// 調用方法的名稱
String methodName = "getTaskList";
String endPoint = "http://asddsyws/services/TaskService.TaskServiceHttpSoap11Endpoint/";
// SOAP Action
String soapAction = "http://asddsyws/services/TaskService/";
// 指定WebService的命名空間和調用方法
SoapObject soapObject = new SoapObject(nameSpace, methodName);
// 設置需要調用WebService接口的參數
soapObject.addProperty("taskType", taskType);
soapObject.addProperty("type", userType);
soapObject.addProperty("clerkNo", userCode);
// 生成調用WebService方法調用的soap信息,並且指定Soap版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.bodyOut = soapObject;
// 是否調用DotNet開發的WebService
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
transport.call(soapAction, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 獲取返回的數據
SoapObject object = (SoapObject) envelope.bodyIn;
// 獲取返回的結果
String result = object.getProperty(0).toString();
Log.i("soap", result);
return result;
}
整體感覺是還蠻簡單的,但是參數一定要配對。接下來介紹下細節(參考大神的文章):
第一:實例化SoapObject 對象,指定webService的命名空間(從相關WSDL文檔中可以查看命名空間),以及調用方法名稱。如:
//命名空間
private static final String serviceNameSpace="http://WebXml.com.cn/";
//調用方法(獲得支持的城市)
private static final String getSupportCity="getSupportCity";
//實例化SoapObject對象
SoapObject request=new SoapObject(serviceNameSpace, getSupportCity);
第二步:假設方法有參數的話,設置調用方法參數
request.addProperty("參數名稱","參數值");
第三步:設置SOAP請求信息(參數部分爲SOAP協議版本號,與你要調用的webService中版本號一致):
//獲得序列化的Envelope
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
第四步:註冊Envelope,
(new MarshalBase64()).register(envelope);
第五步:構建傳輸對象,並指明WSDL文檔URL:
//請求URL
private static final String serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//Android傳輸對象
AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL);
transport.debug=true;
第六步:調用WebService(其中參數爲1:命名空間+方法名稱,2:Envelope對象):
transport.call(serviceNameSpace+getWeatherbyCityName, envelope);
第七步:解析返回數據:
if(envelope.getResponse()!=null){
return parse(envelope.bodyIn.toString());
}
/**************
* 解析XML
* @param str
* @return
*/
private static List<String> parse(String str){
String temp;
List<String> list=new ArrayList<String>();
if(str!=null && str.length()>0){
int start=str.indexOf("string");
int end=str.lastIndexOf(";");
temp=str.substring(start, end-3);
String []test=temp.split(";");
for(int i=0;i<test.length;i++){
if(i==0){
temp=test[i].substring(7);
}else{
temp=test[i].substring(8);
}
int index=temp.indexOf(",");
list.add(temp.substring(0, index));
}
}
return list;
}
總體的步驟都就介紹完全了。
另外,提供兩個soap的服務接口:
a。天氣服務:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
b。電話號碼歸屬服務:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
參考文獻:http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html