适合手机的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);
}
|