Android 調(diào)用 WebService
本節(jié)引言:
經(jīng)過前面的學(xué)習(xí),數(shù)據(jù)請求,數(shù)據(jù)解析,文件上傳下載等,應(yīng)該滿足大家與服務(wù)器交互的基本 需求了,而本節(jié)給大家介紹的Android調(diào)用WebService,其實(shí)這玩意有點(diǎn)類似于一些給我們提供 原始數(shù)據(jù)API服務(wù)的數(shù)據(jù)平臺,比如聚合數(shù)據(jù)!而WebService則用到了XML和SOAP,通過HTTP協(xié)議 即可完成與遠(yuǎn)程機(jī)器的交互!嗯,不多說,開始本節(jié)內(nèi)容~
WebService簡介
PS:如果看完上面簡介還不是很清楚的話,那么就算了,之前公司就用C#搭的一個WebService! 本節(jié)我們并不討論如何去搭建一個WebService,我們僅僅知道如何去獲取WebService提供的服務(wù), 然后解析返回的XML數(shù)據(jù),然后把相關(guān)數(shù)據(jù)顯示到我們的Android設(shè)備上就好!
2.去哪里獲取WebService服務(wù)
網(wǎng)上有很多提供WebService的站點(diǎn),首先找到這些站點(diǎn),然后獲取相應(yīng)的服務(wù)即可! 這里選取WebXml和云聚36wu作為例子給大家講解下,他們的官網(wǎng):
webXml:http://www.webxml.com.cn/zh_cn/index.aspx
以前是免費(fèi)的,不過都商業(yè)化了,很多服務(wù)都要收費(fèi),但是可以試用~ 改站點(diǎn)上提供了16個不同的Web服務(wù),可以根據(jù)自己的需求,查詢相應(yīng)服務(wù),調(diào)用不同的接口!
webXml的相關(guān)頁面:
相關(guān)使用次數(shù)說明:
云聚36wu:http://www.36wu.com/Service
同樣也提供了很多的服務(wù),很多手機(jī)的app都是用的這里的接口,比如彩虹公交,手機(jī)天氣等 不過,這個也是要收費(fèi)的=-=,可以試用,不過只能一小時內(nèi)發(fā)送20次請求; 點(diǎn)擊申請使用,獲得key就可以了!兩者隨便選一個吧!
3.第三方j(luò)ar包的準(zhǔn)備
首先如果想在Android平臺上調(diào)用WebService需要依賴于第三方類庫:ksoap2 而在Android平臺上,使用的是ksoap2 Android,一個高效,輕量級的SOAP開發(fā)包!
jar包下載地址:https://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2
天朝可能上不去,這里提供兩個百度云的鏈接供大家下載使用:
2.54版本:ksoap2-android 2.54.jar
3.30版本:ksoap2-android 3.30.jar
如果所幸你能進(jìn)入jar包的下載地址的話,那么你會看到下面的界面:
4.獲取相關(guān)的一些參數(shù)
首先找到我們需要獲取的服務(wù),然后記錄相關(guān)的參數(shù):NameSpace(命名空間),SoapAction以及URL就不用說了,其他參數(shù)這樣找:
比如我們這里找的是天氣的查詢參數(shù),點(diǎn)進(jìn)去我們可以看到這樣一個參數(shù)文檔:
比如這里我們需要的是天氣查詢部分的功能:
先把框住的SoapAction和NameSpace拷貝下來!當(dāng)然我們可以在這個頁面測試,另外 我們是免費(fèi)用戶,id可以不填直接跳過,輸入后點(diǎn)擊調(diào)用按鈕會打開這樣一個頁面:
嘿嘿,這里就是返回的XML,而我們要做的也就是解析這樣一個XML,另外這里的 .gif代表的是天氣圖標(biāo)!
同理,我們再把歸屬地查詢的看下SoapAction,NameSpace以及相關(guān)參數(shù)mark下!
以及返回后的XML數(shù)據(jù):
5.注冊并啟用相關(guān)WEB服務(wù)
點(diǎn)擊我的Web服務(wù)器,然后點(diǎn)擊試用,WebXML給我們提供了五天的免費(fèi)試用, 我們把需要的兩個服務(wù)器開啟!
好的,記得mark下我們自己的key哦~
6.調(diào)用WebService的代碼示例
嗯,接下來我們來寫代碼驗證調(diào)用WebService的流程:
運(yùn)行效果圖:
PS:這個號碼是以前的號碼=-=,別嘗試撥打,已經(jīng)換人了~ 另外天氣服務(wù)好像有寫問題,有時并不能獲取到,估計是WebXml做的一些限制, 畢竟試用...
實(shí)現(xiàn)代碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText edit_param; private Button btn_attribution; private Button btn_weather; private TextView txt_result; private String city; private String number; private String result; //定義獲取手機(jī)信息的SoapAction與命名空間,作為常量 private static final String AddressnameSpace = "http://WebXml.com.cn/"; //天氣查詢相關(guān)參數(shù) private static final String Weatherurl = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx"; private static final String Weathermethod = "getWeather"; private static final String WeathersoapAction = "http://WebXml.com.cn/getWeather"; //歸屬地查詢相關(guān)參數(shù) private static final String Addressurl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; private static final String Addressmethod = "getMobileCodeInfo"; private static final String AddresssoapAction = "http://WebXml.com.cn/getMobileCodeInfo"; //定義一個Handler用來更新頁面: private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0x001: txt_result.setText("結(jié)果顯示:\n" + result); Toast.makeText(MainActivity.this, "獲取天氣信息成功", Toast.LENGTH_SHORT).show(); break; case 0x002: txt_result.setText("結(jié)果顯示:\n" + result); Toast.makeText(MainActivity.this, "號碼歸屬地查詢成功", Toast.LENGTH_SHORT).show(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); } private void bindViews() { edit_param = (EditText) findViewById(R.id.edit_param); btn_attribution = (Button) findViewById(R.id.btn_attribution); btn_weather = (Button) findViewById(R.id.btn_weather); txt_result = (TextView) findViewById(R.id.txt_result); btn_attribution.setOnClickListener(this); btn_weather.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_weather: new Thread() { @Override public void run() { getWether(); } }.start(); break; case R.id.btn_attribution: new Thread(new Runnable() { public void run() { getland(); } }).start(); break; } } //定義一個獲取某城市天氣信息的方法: public void getWether() { result = ""; SoapObject soapObject = new SoapObject(AddressnameSpace, Weathermethod); soapObject.addProperty("theCityCode:", edit_param.getText().toString()); soapObject.addProperty("theUserID", "dbdf1580476240458784992289892b87"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = soapObject; envelope.dotNet = true; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(Weatherurl); System.out.println("天氣服務(wù)設(shè)置完畢,準(zhǔn)備開啟服務(wù)"); try { httpTransportSE.call(WeathersoapAction, envelope); // System.out.println("調(diào)用WebService服務(wù)成功"); } catch (Exception e) { e.printStackTrace(); // System.out.println("調(diào)用WebService服務(wù)失敗"); } //獲得服務(wù)返回的數(shù)據(jù),并且開始解析 SoapObject object = (SoapObject) envelope.bodyIn; System.out.println("獲得服務(wù)數(shù)據(jù)"); result = object.getProperty(1).toString(); handler.sendEmptyMessage(0x001); System.out.println("發(fā)送完畢,textview顯示天氣信息"); } //定義一個獲取號碼歸屬地的方法: public void getland() { result = ""; SoapObject soapObject = new SoapObject(AddressnameSpace, Addressmethod); soapObject.addProperty("mobileCode", edit_param.getText().toString()); soapObject.addProperty("userid", "dbdf1580476240458784992289892b87"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = soapObject; envelope.dotNet = true; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(Addressurl); // System.out.println("號碼信息設(shè)置完畢,準(zhǔn)備開啟服務(wù)"); try { httpTransportSE.call(AddresssoapAction, envelope); //System.out.println("調(diào)用WebService服務(wù)成功"); } catch (Exception e) { e.printStackTrace(); //System.out.println("調(diào)用WebService服務(wù)失敗"); } //獲得服務(wù)返回的數(shù)據(jù),并且開始解析 SoapObject object = (SoapObject) envelope.bodyIn;//System.out.println("獲得服務(wù)數(shù)據(jù)"); result = object.getProperty(0).toString();//System.out.println("獲取信息完畢,向主線程發(fā)信息"); handler.sendEmptyMessage(0x001); //System.out.println("發(fā)送完畢,textview顯示天氣信息"); } }
另外,別忘了導(dǎo)包和Internet的權(quán)限!
參考代碼下載:
WebServiceDemo.zip:下載 WebServiceDemo.zip
本節(jié)小結(jié):
好的,本節(jié)關(guān)于Android端如何去使用這個WebService就講解到這里,下一節(jié)我們來學(xué)習(xí)一個 類似于瀏覽器的Android控件——WebView,敬請期待~謝謝~!