Android入门:调用WebService


一、Android调用WebServices原理

WebServices通俗的说就是在网络上提供的API,与本地的API不同,我们不能直接调用此方法,而必须按照预先定义的SOAP协议传输给Web服务,然后Web服务接收到XML数据进行处理后,返回XML数据;


发送过去的XML数据中存在需要调用的函数及参数;

接收的XML数据存在函数的返回值,客户端需要从XML数据中解析出结果;


从以上可以看出客户端要做的只是发送XML数据和接收XML数据,因此如果要调用WebService,则客户端的语言是无限制的,可以用C++、Java等任何语言调用Web服务;

相关阅读:在Android中调用WebService【附源码】

二、WebService实例


http://www.webxml.com.cn/zh_cn/index.aspx 

此网址给出了很多Web服务,我们可以调用此处给定的Web服务;

此处我们实现的功能是根据手机号查询归属地;

需要使用的网页为:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo

我们使用SOAP 1.2协议;

从网页中可以看出,我们需要发送如下SOAP协议给Web服务:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  
  3.   <soap12:Body>  
  4.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/">  
  5.       <mobileCode>string</mobileCode>   <!--此处的string可以设置为手机号 -->  
  6.       <userID></userID> <!--此处可以不设置 -->  
  7.     </getMobileCodeInfo>  
  8.   </soap12:Body>  
  9. </soap12:Envelope>  
因此我们先要把此XML数据存到本地文件;


HTTP请求头:

  1. POST /WebServices/MobileCodeWS.asmx HTTP/1.1 //path  
  2. Host: webservice.webxml.com.cn //url  
  3. Content-Type: application/soap+xml; charset=utf-8  
  4. Content-Length: length  

如下HTTP请求头可以看出Web服务的URL为:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx  


接收的XML数据:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  
  3.   <soap12:Body>  
  4.     <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">  
  5.       <getMobileCodeInfoResult>string</getMobileCodeInfoResult><!--用PULL解析出string-->  
  6.     </getMobileCodeInfoResponse>  
  7.   </soap12:Body>  
  8. </soap12:Envelope>  

我们实现一个单元测试用来完成Android客户端调用Web服务;

  1. package org.xiazdong.webservice;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.InputStream;  
  6.   
  7. import org.xmlpull.v1.XmlPullParser;  
  8.   
  9. import android.test.AndroidTestCase;  
  10. import android.util.Xml;  
  11.   
  12. import com.xiazdong.netword.http.util.HttpRequestUtil;  
  13.   
  14. public class WebServiceTest extends AndroidTestCase {  
  15.   
  16.     public void testMobile() throws Exception {  
  17.         InputStream in = this.getClass().getResourceAsStream("mobilesoap.xml");  
  18.         String xml = HttpRequestUtil.read2String(in);  
  19.         xml = xml.replaceAll("string""13795384758");//   
  20.         System.out.println(xml);  
  21.         //发送SOAP,并返回in   
  22.         in = HttpRequestUtil  
  23.                 .postXml(  
  24.                         "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",  
  25.                         xml, "UTF-8");  
  26.         XmlPullParser parser = Xml.newPullParser();  
  27.         parser.setInput(in, "UTF-8");  
  28.         String value = "";  
  29.         int event = parser.getEventType();  
  30.         while (event != XmlPullParser.END_DOCUMENT) {  
  31.             switch (event) {  
  32.             case XmlPullParser.START_TAG:  
  33.                 if ("getMobileCodeInfoResult".equals(parser.getName())) {  
  34.                     value = parser.nextText();//取得   
  35.                 }  
  36.                 break;  
  37.             }  
  38.             event = parser.next();  
  39.         }  
  40.         System.out.println("地址为:"+value);  
  41.     }  
  42. }  

相关内容