?Web Services(Web服務(wù))是一個(gè)用于支持網(wǎng)絡(luò)間不同機(jī)器互操作的軟件系統(tǒng),它是一種自包含、自描述和模塊化的應(yīng)用程序,它可以在網(wǎng)絡(luò)中被描述、發(fā)布和調(diào)用,可以將它看作是基于網(wǎng)絡(luò)的、分布式的模塊化組件。
Web Services是建立在通用協(xié)議的基礎(chǔ)之上,如HTTP、SOAP、UDDI、WSDL等,這些協(xié)議在操作系統(tǒng)、編程語(yǔ)言和對(duì)象模型的選擇上沒(méi)有任何傾向,因此有著很強(qiáng)的生命力。
Web Services的優(yōu)勢(shì)在于提供了不同應(yīng)用程序平臺(tái)之間的互操作,它使得基于組件的開(kāi)發(fā)和Web相結(jié)合的效果達(dá)到最佳。它是基于HTTP協(xié)議的,調(diào)用請(qǐng)求和回應(yīng)消息都可以穿過(guò)防火墻,不需要更改防火墻的設(shè)置,這樣就避免了使用特殊端口進(jìn)行通信時(shí)無(wú)法穿越防火墻的問(wèn)題。
SOAP(Simple Object Access Protocol,簡(jiǎn)單對(duì)象訪問(wèn)協(xié)議)是一種輕量級(jí)的、簡(jiǎn)單的、基于XML的協(xié)議,被設(shè)計(jì)用于在分布式環(huán)境中交換格式化和固化信息的簡(jiǎn)單協(xié)議。也就是說(shuō),要進(jìn)行通信,進(jìn)行數(shù)據(jù)訪問(wèn)傳輸,就必須依賴于一定的協(xié)議,而SOAP正是WebService通信中所依賴的一種協(xié)議。目前經(jīng)常使用的SOAP協(xié)議有兩個(gè)版本:SOAP 1.1 和 SOAP 1.2。
WSDL(Web Services Description Language,即Web服務(wù)描述語(yǔ)言)是一種用來(lái)描述Web服務(wù)的XML語(yǔ)言,它描述了Web服務(wù)的功能、接口、參數(shù)、返回值等,便于用戶綁定和調(diào)用服務(wù)。它以一種和具體語(yǔ)言無(wú)關(guān)的方式定義了給定Web服務(wù)調(diào)用和應(yīng)答的相關(guān)操作和消息。
案例:手機(jī)號(hào)碼歸屬地查詢
activity_main.xml
<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="手機(jī)號(hào)碼(段):"? ? ? />
<EditText?
android:id="@+id/phone_sec"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPhonetic"
android:singleLine="true"
android:hint="例如:1875337"
/>
<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>
、、、
MainActivity.java
、、、
package com.example.telphone;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.DownloadManager.Query;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private EditText phoneSecEditText;
private TextView resultView;
private Button queryButton;
String result;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==0x123){
resultView.setText(result);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneSecEditText = (EditText) findViewById(R.id.phone_sec);
resultView = (TextView) findViewById(R.id.result_text);
queryButton = (Button) findViewById(R.id.query_btn);
queryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 手機(jī)號(hào)碼(段)
final String phoneSec = phoneSecEditText.getText().toString().trim();
// 簡(jiǎn)單判斷用戶輸入的手機(jī)號(hào)碼(段)是否合法
if ("".equals(phoneSec) || phoneSec.length() < 7) {
// 給出錯(cuò)誤提示
phoneSecEditText.setError("您輸入的手機(jī)號(hào)碼(段)有誤!");
phoneSecEditText.requestFocus();
// 將顯示查詢結(jié)果的TextView清空
resultView.setText("");
return;
}
// 查詢手機(jī)號(hào)碼(段)信息
new Thread(){
public void run() {
getRemoteInfo(phoneSec);
};
}.start();
}
});
}
/**
* 手機(jī)號(hào)段歸屬地查詢
*
* @param phoneSec 手機(jī)號(hào)段
*/
public void getRemoteInfo(String phoneSec)? {
// 命名空間
String nameSpace = "http://WebXml.com.cn/";
// 調(diào)用的方法名稱
String methodName = "getMobileCodeInfo";
// EndPoint
String endPoint = " http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
// SOAP Action
String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
// 指定WebService的命名空間和調(diào)用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 設(shè)置需調(diào)用WebService接口需要傳入的兩個(gè)參數(shù)mobileCode、userId
rpc.addProperty("mobileCode", phoneSec);
rpc.addProperty("userId", "");
// 生成調(diào)用WebService方法的SOAP請(qǐng)求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = rpc;
// 設(shè)置是否調(diào)用的是dotNet開(kāi)發(fā)的WebService
envelope.dotNet = true;
// 等價(jià)于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 調(diào)用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 獲取返回的數(shù)據(jù)
SoapObject object = (SoapObject) envelope.bodyIn;
// 獲取返回的結(jié)果
result = object.getProperty(0).toString();
// 將WebService返回的結(jié)果顯示在TextView中
//resultView.setText(result);
// Message message = new Message();
handler.sendEmptyMessage(0x123);
}
}
、、、