歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android入門:調用WebService

Android入門:調用WebService

日期:2017/3/1 10:15:57   编辑:Linux編程

一、Android調用WebServices原理

WebServices通俗的說就是在網絡上提供的API,與本地的API不同,我們不能直接調用此方法,而必須按照預先定義的SOAP協議傳輸給Web服務,然後Web服務接收到XML數據進行處理後,返回XML數據;


發送過去的XML數據中存在需要調用的函數及參數;

接收的XML數據存在函數的返回值,客戶端需要從XML數據中解析出結果;


從以上可以看出客戶端要做的只是發送XML數據和接收XML數據,因此如果要調用WebService,則客戶端的語言是無限制的,可以用C++、Java等任何語言調用Web服務;

相關閱讀:在Android中調用WebService【附源碼】http://www.linuxidc.com/Linux/2012-03/56257.htm

二、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. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import org.xmlpull.v1.XmlPullParser;
  6. import android.test.AndroidTestCase;
  7. import android.util.Xml;
  8. import com.xiazdong.netword.http.util.HttpRequestUtil;
  9. public class WebServiceTest extends AndroidTestCase {
  10. public void testMobile() throws Exception {
  11. InputStream in = this.getClass().getResourceAsStream("mobilesoap.xml");
  12. String xml = HttpRequestUtil.read2String(in);
  13. xml = xml.replaceAll("string", "13795384758");//
  14. System.out.println(xml);
  15. //發送SOAP,並返回in
  16. in = HttpRequestUtil
  17. .postXml(
  18. "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",
  19. xml, "UTF-8");
  20. XmlPullParser parser = Xml.newPullParser();
  21. parser.setInput(in, "UTF-8");
  22. String value = "";
  23. int event = parser.getEventType();
  24. while (event != XmlPullParser.END_DOCUMENT) {
  25. switch (event) {
  26. case XmlPullParser.START_TAG:
  27. if ("getMobileCodeInfoResult".equals(parser.getName())) {
  28. value = parser.nextText();//取得
  29. }
  30. break;
  31. }
  32. event = parser.next();
  33. }
  34. System.out.println("地址為:"+value);
  35. }
  36. }
Copyright © Linux教程網 All Rights Reserved