關於webservices的介紹在W3C上講的很詳細。webservice就是一個網路應用程式,基本的WebService平台是XML+HTTP。
- HTTP 協議是最常用的網際網路協議。
- XML 提供了一種可用於不同的平台和程式設計語言之間的語言。
Web services 平台的元素:
- SOAP (簡易對象訪問協議) :基於 XML,獨立於語言,通過 HTTP 在應用程式間交換資訊。
- UDDI (通用描述、發現及整合):由 WSDL 描述的網路服務介面目錄,通過它,企業可註冊並搜尋 Web services。
- WSDL (Web services 描述語言):基於 XML 的用來描述 Web services 以及如何訪問它們的一種語言。
webService的使用
通過表單和 HTTP POST,將 web service 置於網站上提供服務。通過XML 在應用程式間發送訊息。
通過使用WebService,我們能夠像調用本地方法一樣去調用遠程伺服器上的方法。並不需要關心伺服器是用什麼寫的 ,基於什麼平台。
這個應用主要是利用http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx的號碼歸屬地WebService來實現歸屬地的查詢:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="5dip" android:paddingLeft="5dip" android:paddingRight="5dip" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="請輸入要查詢的手機號碼:" /> <EditText android:id="@+id/phone_sec" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPhonetic" android:singleLine="true" android:hint="例如:1398547" /> <Button android:id="@+id/query_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="查詢" /> <TextView android:id="@+id/result_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" /> </LinearLayout>
CODE:
這裡面對於簡單的EditText輸入校正就不做了,另外這裡需要搞清楚nameSpace ,methodName,asmx,Action變數,在http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL說明文檔中,都寫的很清除了asmx就是WSDL文檔去掉後面的參數,實踐過程中,發現帶上也可以請求到資料。Action就是 命名空間 加上 方法名。
/** * 手機號段歸屬地查詢 * * @param phoneSec 手機號段 */ public void getRemoteInfo(String phoneSec) { // 命名空間 String nameSpace = "http://WebXml.com.cn/"; // 調用的方法名稱 String methodName = "getMobileCodeInfo"; // asmx String asmx = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // SOAP Action String soapAction = "http://WebXml.com.cn/getMobileCodeInfo"; // 指定WebService的命名空間和調用的方法名 SoapObject sobj = new SoapObject(nameSpace, methodName); // 設定需調用WebService介面需要傳入的兩個參數mobileCode、userId sobj.addProperty("mobileCode", phoneSec); sobj.addProperty("userId", ""); // 產生調用WebService方法的SOAP請求資訊,並指定SOAP的版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); envelope.bodyOut = sobj; // 設定是否調用的是dotNet開發的WebService envelope.dotNet = true; // 等價於envelope.bodyOut = rpc; envelope.setOutputSoapObject(sobj); HttpTransportSE transport = new HttpTransportSE(asmx); try { // 調用WebService transport.call(soapAction, envelope); } catch (Exception e) { e.printStackTrace(); } // 擷取返回的資料 SoapObject object = (SoapObject) envelope.bodyIn/*.getResponse()*/; // 擷取返回的結果 String result = object.getProperty("getMobileCodeInfoResult"/*0*/).toString(); // 將WebService返回的結果顯示在TextView中 resultView.setText(result); }
看到上面擷取對象和擷取結果可以有兩種方式。都是saop中提供的方法。
如果在擷取到的放回資料 有多個值 ,可以通過result.getPropertyCount() 來擷取所的結果的個數
在某些時候,可能會報一些莫名異常,可以嘗試修改sopa版本 ,或者將action設為null(有些不需要action)
許可權:
最後一件事 :不要忘記加許可權
<uses-permission android:name="android.permission.INTERNET" />