Android中調用Web Services有不少方法,咱們如今使用的是ksoap,它是SOAP web services的客戶端包,ksoap如今版本爲2.0.它的一個主要優勢就是對dotNET兼容性比較不錯。android
首先下載ksoap的包文件(下載地址),在Eclispe的Package Explorer中右鍵項目,Build Path>Add Libraries,找到ksoap2-android-assembly-2.4-jar-with-dependencies.jar添加該引用。代碼以下:web
public
class
WSHelper {
/*************************************
* 獲取web services內容
* @param url
* @param params
* @return
*************************************/
public
static
String GetResponse(String method,List<BasicNameValuePair> params){
try
{
String url = WSUrl;
SoapObject request =
new
SoapObject(namespace, method);
for
(
int
i=
0
,len=params.size();i<len;i++){
request.addProperty(params.get(i).getName(), params.get(i).getValue());
}
SoapSerializationEnvelope envelope =
new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet =
true
;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport =
new
AndroidHttpTransport(url);
androidHttpTransport.call(namespace + method, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
return
result.toString();
}
catch
(Exception e) {
return
"Error:calling the web services error"
;
}
}
}
調用時代碼以下:ui
String method =
"MethodName"
;
//方法名稱
url
List<BasicNameValuePair> params =
new
ArrayList<BasicNameValuePair>();
spa
params.add(
new
BasicNameValuePair(
"userId"
, String.valueOf(
1995
)));
return
WSHelper.GetResponse(method,params);
.net