Android WebService 使用小结

适合手机的WebService客户端SDK   KSOAP2:

http://code.google.com/p/ksoap2-android/downloads/list

http://files.cnblogs.com/xiaobuild/KSOAP2_SDK.rar

将下载后的jar文件复制到工程的lib目录中(如果没有该目录,可以新建一个,当然,也可以放在其他的目录中)。并在工程中引用这个jar包

如何调用WebService

打开webServic链接,如http://macau.sanvio.net/ReadOrderWebService/wsReadOrder.asmx?op=GetNews

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地址调用参数等

 

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";

具体代码如下:

 

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();
              }

一层一层遍历信息:

 

  /**
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);
}

你可能感兴趣的:(Android WebService 使用小结)