適合手機的WebService客戶端SDK KSOAP2: java
http://code.google.com/p/ksoap2-android/downloads/list android
http://files.cnblogs.com/xiaobuild/KSOAP2_SDK.rar web
將下載後的jar文件複製到工程的lib目錄中(若是沒有該目錄,能夠新建一個,固然,也能夠放在其餘的目錄中)。並在工程中引用這個jar包 ui
如何調用WebService google
打開webServic連接,如http://macau.sanvio.net/ReadOrderWebService/wsReadOrder.asmx?op=GetNews spa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
POST /ReadOrderWebService/wsReadOrder.asmx HTTP/1.1
Host: macau.sanvio.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "
http://tempuri.org/GetNews"
<?xmlversion="1.0"encoding="utf-8"?>
<soap:Envelopexmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="
http://www.w3.org/2001/XMLSchema"xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetNewsxmlns="
http://tempuri.org/">
<newsid>string</newsid>
</GetNews>
</soap:Body>
</soap:Envelope>\
|
根據該xml肯定命名空間 ,調用方法, webServer地址,調用參數等 .net
1
2
3
4
5
6
|
// 命名空間
privatestaticfinalString ServiceNameSpace ="
http://tempuri.org/";
// webServer地址
privatestaticfinalString webServiceUrl ="
http://macau.sanvio.net/ReadOrderWebService/wsReadOrder.asmx";
//webServer調用方法名
privatestaticfinalString GetNews ="GetNews";
|
具體代碼以下: debug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// 實例化SoapObject對象
SoapObject request =newSoapObject(ServiceNameSpace, GetNews);
// 有參數的話,設置調用方法參數 request.addProperty("參數名稱","參數值");
request.addProperty("newsid","0001");
// 得到序列化的Envelope 註冊Envelope
SoapSerializationEnvelope envelope =newSoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet =true;
envelope.setOutputSoapObject(request);// envelope.bodyOut=request;
// HttpTransportSE傳輸對象
HttpTransportSE transport =newHttpTransportSE(webServiceUrl);
transport.debug =true;
// 調用
try{
transport.call(ServiceNameSpace + GetNews, envelope);
if(envelope.getResponse() !=null) {
System.out.println("Get Info is finisth!");
// System.out.println(envelope.bodyIn.toString());
// parseNews(envelope.bodyIn.toString());
//write(envelope.bodyIn.toString(), "001.txt");
//解析xml
parseNews(envelope);
}
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
|
一層一層遍歷信息: code
/**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
*
* SoapSerializationEnvelope準換爲SoapObject 遍歷SoapObject 解析xml
*
*@paramenvelope
*@throwsIOException
*/
publicvoidparseNews(SoapSerializationEnvelope envelope) {
strResult ="";
SoapObject soapObject = (SoapObject) envelope.bodyIn;
for(inti =0; i < soapObject.getPropertyCount(); i++) {
SoapObject soapChilds = (SoapObject) soapObject.getProperty(i);
for(intj =1; j < soapChilds.getPropertyCount(); j++) {
SoapObject soapChilds2 = (SoapObject) soapChilds.getProperty(j);
for(intk =0; k < soapChilds2.getPropertyCount(); k++) {
SoapObject soapChilds3 = (SoapObject) soapChilds2
.getProperty(k);
for(intl =0; l < soapChilds3.getPropertyCount(); l++) {
SoapObject soapChilds4 = (SoapObject) soapChilds3
.getProperty(l);
strResult += soapChilds4.getPropertyCount()+" "+soapChilds4.getAttributeCount()
+"\n========================================\n";
for(intn =0; n<soapChilds4.getPropertyCount(); n++) {
strResult +="\n"+soapChilds4.getProperty(n);
}
}
}
}
}
showTextView.setText(strResult);
}
|