• 
    

    
    

      99热精品在线国产_美女午夜性视频免费_国产精品国产高清国产av_av欧美777_自拍偷自拍亚洲精品老妇_亚洲熟女精品中文字幕_www日本黄色视频网_国产精品野战在线观看

      ?

      Android開發(fā)中Web Service網(wǎng)絡編程研究

      2012-04-29 05:47:22巫志勇
      電腦知識與技術 2012年20期

      巫志勇

      摘要:該文介紹了在Android開發(fā)中Web Service功能的實現(xiàn)。并設計了一個國內(nèi)手機號碼歸屬地查詢的程序,研究通過Web Ser? vice遠程調(diào)用來獲取數(shù)據(jù)的方式。

      關鍵詞:Android;Web Service;ksoap2

      中圖分類號:TP393文獻標識碼:A文章編號:1009-3044(2012)20-4904-03

      In the Development of Android Web Service Network Programming Research

      WU Zhi-yong

      (Guangdong Female Polytechnic College, Guangzhou 511450, China)

      Abstract: This paper describes the implement of Web Services functionality on Android platform. And design a program for inquiries to phone numbers attribution, to show the way to remote calls Web Service function.

      Key words: Android; Web Service; ksoap2

      Web Service是一種面向服務架構(gòu)(Service-oriented architecture,SOA)的技術,目的是實現(xiàn)不同平臺的應用服務之間的相互調(diào)用。Android作為一個市場占有率第一的移動操作系統(tǒng),其網(wǎng)絡功能是最重要的特性之一。在Android開發(fā)中通過Web Service可以方便地實現(xiàn)不同平臺之間的方法調(diào)用,從網(wǎng)上獲取數(shù)據(jù)信息和實現(xiàn)功能擴展。Web Service通過標準的Web協(xié)議提供服務。

      通過Web Service實現(xiàn)遠程方法調(diào)用,獲取數(shù)據(jù)信息,最關鍵的問題是數(shù)據(jù)訪問和傳輸?shù)膮f(xié)議規(guī)范。

      SOAP協(xié)議(Simple Object Access Protocal,簡單對象訪問協(xié)議),它是一個分布式網(wǎng)絡環(huán)境下用于信息交換的通訊協(xié)議。在此協(xié)議下,應用程序和軟件組件可以通過標準的Web協(xié)議進行通訊。SOAP使用基于XML的可擴展消息格式,需同時綁定一個傳輸用協(xié)議。這個協(xié)議通常是HTTP或HTTPS,但也可以使用SMTP或XMPP。

      WSDL是一個XML格式文檔,用以描述服務端口訪問方式和使用協(xié)議的細節(jié)。通常用來輔助生成服務器和客戶端代碼及配置信息。

      UDDI是用來發(fā)布和搜索WEB服務的協(xié)議,應用程序可藉由此協(xié)議在設計或運行時找到目標WEB服務。

      Java開發(fā)中的Web Service有很多種實現(xiàn)方式,如XML-RPC、XFile、Axis等等,可是這些庫并不適合資源有限的Android手機客戶端。在Java ME版本中,廣泛使用的是KSOAP。雖然Android并不使用Java ME,但是KSOAP也有Android下的可用版本ksoap2-Android。

      2.1 ksoap2-Android

      kSOAP是Enhydra.org的一個開源作品,是EnhydraME項目的一部分。ksoap2-Android是ksoap2在Android下的一個移植版本,利用它可以非常方便地訪問Web Service。ksoap2的常用接口有:

      org.ksoap2. SoapObject

      org.ksoap2. SoapEnvelope

      org.ksoap2. SoapSerializationEnvelope

      org.ksoap2.transport. HttpTransport

      SoapObject用于創(chuàng)建SOAP對象,實現(xiàn)SOAP調(diào)用;

      SoapEnvelope實現(xiàn)了SOAP標準中的SOAP Envelope,封裝了head對象和body對象。

      SoapSerializationEnvelope是ksoap2中對SoapEnvelope的擴展,支持SOAP序列化(Serialization)格式規(guī)范,可以對簡單對象自動進行序列化(Simple object serialization)。

      HttpTransport用于進行Internet訪問/請求,獲取服務器SOAP。

      2.2 ksoap2-Android的編譯配置

      圖1

      <TextView

      android:layout_width="fill_parent"

      android:layout_height="wrap_content" android:text="@string/phonenumber" /><EditText

      android:id="@+id/EditTextPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" >

      <requestFocus /></EditText><Button

      android:id="@+id/btnCheck"

      android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnCheck" />

      3.2查詢的代碼

      當輸入手機號碼,點擊查詢按鈕時,訪問Web Service,遠程調(diào)用getMobileCodeInfo方法來獲得查詢結(jié)果。訪問Web Service的步驟如下:

      1)實例化SoapObject對象,指定webService的命名空間和調(diào)用的方法名稱。

      String serviceNameSpace = "http://WebXml.com.cn/";

      String methodName = "getMobileCodeInfo";

      SoapObject request = new SoapObject(serviceNameSpace,methodName);

      2)設置調(diào)用方法的參數(shù)。根據(jù)服務提供方描述的參數(shù)要求,需要兩個參數(shù)。第一個為mobileCode,字符串類型,表示手機號碼,第二個為userID為商業(yè)用戶ID,免費用戶為空字符串。

      request.addProperty("mobileCode", strPhoneNumber);

      request.addProperty("userID", "");

      3)通過實例化Envelope對象,設置SOAP請求信息,并注冊Envelope對象。對應于不同的SOAP規(guī)范,需要使用不同的常量作為參數(shù)。常量SoapEnvelope.VER10:對應于SOAP 1.0規(guī)范;常量SoapEnvelope.VER11:對應于SOAP 1.1規(guī)范;常量SoapEnvelope. VER12:對應于SOAP 1.2規(guī)范。

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

      envelope.bodyOut = request;

      (new MarshalBase64()).register(envelope);

      4)實例化傳輸對象,參數(shù)為傳遞SOAP數(shù)據(jù)的目標地址,即WSDL文檔的URL。HttpTransportSE是一個強大的Http傳輸類,可以完成Http傳輸過程。它封裝了網(wǎng)絡請求的一切,可以完全不用考慮序列化消息。通過把它的debug屬性設置為true來啟用調(diào)試信息。

      String serviceURL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

      HttpTransportSE transport = new HttpTransportSE(serviceURL);

      transport.debug = true;

      5)獲取返回結(jié)果,并進行解析。

      返回數(shù)據(jù)為字符串(手機號碼:省份城市手機卡類型)。

      if(envelope.getResponse()!=null){

      return (envelope.bodyIn.toString());

      }

      在Android開發(fā)中使用ksoap2-Android可以很方便地實現(xiàn)Web Service功能,通過Web Service遠程調(diào)用來獲取數(shù)據(jù)。但是在開發(fā)中發(fā)現(xiàn),直接訪問Web Service來獲取數(shù)據(jù),會由于網(wǎng)絡的延時而造成界面響應的短暫停頓,應該研究更好的方法,如通過后臺異步數(shù)據(jù)傳輸來解決。

      吉水县| 尉犁县| 宝坻区| 玉门市| 和平县| 阿城市| 富民县| 固原市| 颍上县| 贡觉县| 泾阳县| 阳信县| 汽车| 松江区| 福建省| 平塘县| 梧州市| 彰化县| 调兵山市| 海晏县| 丹凤县| 衡阳市| 林周县| 香港 | 上杭县| 斗六市| 含山县| 名山县| 高台县| 邢台县| 饶河县| 霍邱县| 韶山市| 沂源县| 奎屯市| 边坝县| 根河市| 乌苏市| 苏尼特右旗| 榆树市| 新龙县|