在Android中使用Android Ksoap2調用WebService,androidksoap2
WebService是基於SOAP協議可實現web伺服器與web伺服器之間的通訊,因採用SOAP協議傳送XML資料具有平台無關性,也是成為解決異構平台之間通訊的重要解決方案,比如Java平台與.net平台之間。因此在web應用中有著舉足輕重的作用,很多機構、組織都在各自平台上對外發布了WebService(例如:天氣預報、航班資訊、股市行情等等),這樣任何平台和客戶都可以享受到這些服務,當然有些是要付費的。
對於Android端調用WebService,有兩種方式,一種自己編寫代碼主要通過URL獲得 HttpUrlConnection的方式建立與webservice的串連,然後進行I/O讀寫傳送和獲得資料,並對獲得資料進行XML解析,比較麻煩。另一種就是使用第三方組件,比較常用的就是ksoap2-android。
ksoap2-android這個開源組件針對Android平台提供了一個輕量級和高效的SOAP類庫,可方便實現Android端與WebService之間的通訊
1、環境搭建
ksoap2-android項目的地址:http://code.google.com/p/ksoap2-android/ 大家可以下載最新版本jar,然後將jar加入到項目中即可。
我這裡使用是ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar
2、Ksoap2 使用的主要步驟
1)web服務參數準備
// webservice服務地址String url= “http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx”;//web服務的命名空間String namespace=” http://WebXml.com.cn/”;//請求服務的方法名稱String methodName=”getMobileCodeInfo”;//soap請求地址String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo";
2)建立HttpTransportSE,該組件可發送請求
HttpTransportSE transport = new HttpTransportSE(url);
3)建立SoapObject,添加要傳送的資料(資訊載體)
SoapObject soapObject = new SoapObject(namespace,methodName);soapObject.addProperty(name,value);//添加資料…
4)建立SoapSerializationEnvelope對象,指定xml版本,以及request中body
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = soapObject;envelope.setOutputSoapObject(soapObject);
5)發送請求,調用webserivce中的方法
httpTransportSE.call(soapActionAddress, envelope);//服務傳回的資訊,會放在envelope的bodyIn屬性中
6) 擷取服務傳回的資料
SoapObject object = (SoapObject) envelope.bodyIn;
| 三、實現案例——通過調用webservice查詢手機號碼的歸屬地 |
執行效果如下:
完整代碼實現:
public class MainActivity extends Activity { ///手機歸屬地Webservice的參數資訊 private static final String nameSpaceAddress = "http://WebXml.com.cn/";private static final String urlAddress = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; private static final String methodNameAddress = "getMobileCodeInfo"; private static final String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo"; private TextView telAddress = null; private EditText tel = null; private Button btnAddress = null;protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnAddress = (Button) this.findViewById(R.id.btnSearchAddress); telAddress = (TextView) this.findViewById(R.id.telAddress); tel = (EditText) this.findViewById(R.id.telNo); btnAddress.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { public void run() { getTelAddress(); } }).start(); } });/*** 請求WebService並獲得返回的手機號碼歸屬地資訊*/public void getTelAddress() { SoapObject soapObject = new SoapObject(nameSpaceAddress, methodNameAddress);//建立SOAP對象 //設定屬性,這些屬性值通過SOAP協議傳送給伺服器 soapObject.addProperty("mobileCode", tel.getText().toString());//要查詢的電話號碼 soapObject.addProperty("userId", ""); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = soapObject; envelope.dotNet = true; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(urlAddress); try { //調用服務 httpTransportSE.call(soapActionAddress, envelope); } catch (Exception e) { e.printStackTrace(); } //擷取服務傳回的資料,手機歸屬地資訊 SoapObject object = (SoapObject) envelope.bodyIn; txtAddress = object.getProperty(0).toString(); //向主線程發送訊息成功,getTelAddress函數執行完畢 handlerAddress.sendEmptyMessage(0); } Handler handlerAddress = new Handler() { public void handleMessage(Message msg) { telAddress.setText(txtAddress); Toast.makeText(MainActivity.this, "擷取號碼歸屬地成功"+txtAddress, Toast.LENGTH_LONG).show(); } };}
手機歸屬地服務
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
天氣預報Web服務,資料來源於中國氣象局
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
IP地址來:
http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
中文 <-> 英文雙向翻譯 WEB 服務:
http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx
火車時刻表
http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx
航班查詢服務
http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx
中國股票行情資料 WEB 服務
http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx
中國電視節目預告
http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx