1.WebService簡介

PS:如果看完上面簡介還不是很清楚的話,那么就算了,之前公司就用C#搭的一個WebService! 本節我們並不討論如何去搭建一個WebService,我們僅僅知道如何去獲取WebService提供的服務, 然后解析返回的XML數據,然后把相關數據顯示到我們的Android設備上就好!
2.去哪里獲取WebService服務
網上有很多提供WebService的站點,首先找到這些站點,然后獲取相應的服務即可! 這里選取WebXml和雲聚36wu作為例子給大家講解下,他們的官網:
webXml:http://www.webxml.com.cn/zh_cn/index.aspx
以前是免費的,不過都商業化了,很多服務都要收費,但是可以試用~ 改站點上提供了16個不同的Web服務,可以根據自己的需求,查詢相應服務,調用不同的接口!
webXml的相關頁面:

相關使用次數說明:

雲聚36wu:http://www.36wu.com/Service
同樣也提供了很多的服務,很多手機的app都是用的這里的接口,比如彩虹公交,手機天氣等 不過,這個也是要收費的=-=,可以試用,不過只能一小時內發送20次請求; 點擊申請使用,獲得key就可以了!兩者隨便選一個吧!

3.第三方jar包的准備
首先如果想在Android平台上調用WebService需要依賴於第三方類庫:ksoap2 而在Android平台上,使用的是ksoap2 Android,一個高效,輕量級的SOAP開發包!
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
如果所幸你能進入jar包的下載地址的話,那么你會看到下面的界面:
4.獲取相關的一些參數
首先找到我們需要獲取的服務,然后記錄相關的參數: NameSpace(命名空間),SoapAction以及URL就不用說了,其他參數這樣找:
比如我們這里找的是天氣的查詢參數,點進去我們可以看到這樣一個參數文檔:
比如這里我們需要的是天氣查詢部分的功能:
先把框住的SoapAction和NameSpace拷貝下來!當然我們可以在這個頁面測試,另外 我們是免費用戶,id可以不填直接跳過,輸入后點擊調用按鈕會打開這樣一個頁面:
嘿嘿,這里就是返回的XML,而我們要做的也就是解析這樣一個XML,另外這里的 .gif代表的是天氣圖標!
同理,我們再把歸屬地查詢的看下SoapAction,NameSpace以及相關參數mark下!

以及返回后的XML數據:

5.注冊並啟用相關WEB服務


點擊我的Web服務器,然后點擊試用,WebXML給我們提供了五天的免費試用, 我們把需要的兩個服務器開啟!

好的,記得mark下我們自己的key哦~
6.調用WebService的代碼示例
嗯,接下來我們來寫代碼驗證調用WebService的流程:
運行效果圖:

PS:這個號碼是以前的號碼=-=,別嘗試撥打,已經換人了~ 另外天氣服務好像有寫問題,有時並不能獲取到,估計是WebXml做的一些限制, 畢竟試用...
實現代碼:
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;
//定義獲取手機信息的SoapAction與命名空間,作為常量
private static final String AddressnameSpace = "http://WebXml.com.cn/";
//天氣查詢相關參數
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";
//歸屬地查詢相關參數
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("結果顯示:\n" + result);
Toast.makeText(MainActivity.this, "獲取天氣信息成功", Toast.LENGTH_SHORT).show();
break;
case 0x002:
txt_result.setText("結果顯示:\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("天氣服務設置完畢,准備開啟服務");
try {
httpTransportSE.call(WeathersoapAction, envelope);
// System.out.println("調用WebService服務成功");
} catch (Exception e) {
e.printStackTrace();
// System.out.println("調用WebService服務失敗");
}
//獲得服務返回的數據,並且開始解析
SoapObject object = (SoapObject) envelope.bodyIn;
System.out.println("獲得服務數據");
result = object.getProperty(1).toString();
handler.sendEmptyMessage(0x001);
System.out.println("發送完畢,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("號碼信息設置完畢,准備開啟服務");
try {
httpTransportSE.call(AddresssoapAction, envelope);
//System.out.println("調用WebService服務成功");
} catch (Exception e) {
e.printStackTrace();
//System.out.println("調用WebService服務失敗");
}
//獲得服務返回的數據,並且開始解析
SoapObject object = (SoapObject) envelope.bodyIn;//System.out.println("獲得服務數據");
result = object.getProperty(0).toString();//System.out.println("獲取信息完畢,向主線程發信息");
handler.sendEmptyMessage(0x001);
//System.out.println("發送完畢,textview顯示天氣信息");
}
}
另外,別忘了導包和Internet的權限!
<uses-permission android:name="android.permission.INTERNET"/>






