android之使用Soap協議調用webservice實現手機歸屬地查詢

 一:Web服務(webservice)是局域網和因特網上能夠支持機器與機器之間互操作的軟件系統。它有一個用WSDL描述的接口,其它系統可以使用SOAP消息以接口所描述的方式與之交互。SOAP協議是Web服務賴以生存的基礎。

      Web服務的目標是實現在這樣的分佈式環境環境中,各個組織內部及各組織之間任意數量的應用程序或應用程序組件能夠以與平臺無關和語言無關的方式無縫交互。
Web服務是通過統一資源標識URI標識的軟件系統,它的共用接口和綁定用XML來定義和描述。軟件系統可以通過Internet協議傳遞基於XML的消息,這樣就可以用Web Service 所定義的方式與其交互。
       Web服務使我們能夠對因特網或網絡上的一個對象進行遠程調用RPC(Remote Procedure Call)。Web服務使用中性平臺標準(HTTP和XML),對客戶完全隱藏執行任務的細節,客戶只需要知道這個服務的URL或方法調用使用的數據類型,爲不同平臺提供服務。
       SOAP、WSDL、和UDDI是webservice技術體系的核心:

               (1)WSDL是Web服務的描述語言,它類似於CORBA的IDL用以描述Web服務的交互消息格式、端口類型以及傳輸協議的綁定。

               (2)Web服務使用UDDI作爲目錄機制,服務發佈者可以將服務信息註冊到UDDI,從而方便服務使用者進行服務查找。

               (3)SOAP提供一個標準的包裝結構用以在多種標準Internet技術上(包括SMTP、HTTP和FTP)傳輸XML文檔。它還定義了用XML傳送非XML RPC調用的編碼和綁定標準,SOAP爲RPC提供了一個簡單的結構:文檔交換。採用標準傳輸機制後,異構的客戶和服務器能一下子可互操作。

二:SOAP(Simple Object Access Protocol)即簡單對象訪問協議,它是一個輕型分佈式計算協議,允許在一個分散、分佈的環境交換結構化的信息。SOAP規範定義了在分佈式系統中傳送消息的框架和支持遠程過程調用和響應的慣例。SOAP消息是以SOAP Envelope爲根元素,內含2個子元素:SOAP HeaderSOAP Body。SOAP Body是強制性的,是SOAP消息必須要有的元素,它包含了SOAP消息的主要內容,由最終接收SOAP消息的節點處理。SOAP Body是應用的有效載荷,可以包含應用數據、RPC方法和參數以及SOAP錯誤。

三:下面是android開發中基於soap協議與服務器交互實現手機歸屬地查詢案例  。

1。運行效果:

    

不存在此號用戶時:

 

2.佈局文件代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/map" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="手機號碼:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="@+id/text"
        android:layout_marginTop="80dp"
        android:layout_toRightOf="@+id/text" >
    </EditText>

    <Button
        android:id="@+id/search"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/phone"
        android:text="查詢" />

    <TextView
        android:id="@+id/res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="250dp"
        android:text="查詢結果:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="250dp"
        android:layout_toRightOf="@+id/res" />

</RelativeLayout>


3.創建手機歸屬地查詢工具類PhoneUtil.java

public class PhoneUtil {

	public static Object searchPlace(String wsdlurl,String method,String phonenumber) {
		//指定webservice的命名空間和調用的方法名
		String namespace="http://WebXml.com.cn/";
		SoapObject  soap=new SoapObject(namespace,method);
		//添加屬性,只要設置參數的順序一致,調用方法的參數名不一定與服務端的WebService類中的方法參數名一致
		soap.addProperty("mobileCode",phonenumber);
		soap.addProperty("userID", null);
		//通過SoapSerializationEnvelope類的構造方法設置SOAP協議的版本號。 
		SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		//設置需要傳出的Soap
		soapEnvelope.bodyOut=soap;
		soapEnvelope.dotNet=true;
		soapEnvelope.setOutputSoapObject(soap);
		//創建http傳輸對象
		HttpTransportSE transportSE=new HttpTransportSE(wsdlurl);
		//soap操作url
		String SOAP_ACTION=namespace+method;
		try {
			//請求調用WebService方法
			transportSE.call(SOAP_ACTION, soapEnvelope);
			//使用getResponse獲得WebService方法解析xml的返回結果
			Object result=soapEnvelope.getResponse();
			if(result!=null)
				return result;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return null;
	}

}

4.MainActivity類

public class MainActivity extends Activity {
	private EditText phonenum;
	private Button search;
	private EditText result;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //設置無標題欄
        //setTheme(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //查找組件
        phonenum=(EditText) this.findViewById(R.id.phone);
        search=(Button) this.findViewById(R.id.search);
        result=(EditText) this.findViewById(R.id.result);
        search.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String phone=phonenum.getText().toString();
				//web服務端手機歸屬地url
				String wsdlUrl="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
				//調用web提供的方法getMobileCodeInfo得到歸屬地
				Object answer=PhoneUtil.searchPlace(wsdlUrl,"getMobileCodeInfo",phone);
				//查詢結果顯示在結果文本域
				result.setText(answer.toString());
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章