Android 通過WebService進行網路程式設計,使用工具類輕鬆實現
相信大家在平常的開發中,對網路的操作用到HTTP協議比較多,通過我們使用Get或者Post的方法呼叫一個數據介面,然後伺服器給我們返回JSON格式的資料,我們解析JSON資料然後展現給使用者,相信很多人很喜歡伺服器給我們返回JSON資料格式,因為他解析方便,也有一些JSON的解析庫,例如Google提供的GSON,阿里巴巴的FastJson,不過還是推薦大家使用FastJson來解析,我自己開發中也是用FastJson來解析,FastJson的介紹http://code.alibabatech.com/wiki/display/FastJSON/Home,不過有時候我們用到WebService介面來獲取資料, WebService是一種基於SOAP協議的遠端呼叫標準,通過webservice可以將不同作業系統平臺、不同語言、不同技術整合到一塊。在Android SDK中並沒有提供呼叫WebService的庫,因此,需要使用第三方的SDK來呼叫WebService。PC版本的WEbservice客戶端庫非常豐富,例如Axis2,CXF等,但這些開發包對於Android系統過於龐大,也未必很容易移植到Android系統中。因此,這些開發包並不是在我們的考慮範圍內。適合手機的WebService客戶端的SDK有一些,比較常用的有Ksoap2,可以從
我們新建一個WebService的工具類,用於對WebService介面的呼叫,以後遇到呼叫WebService直接拷貝來用就行了
package com.example.webservicedemo; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpResponseException; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.os.Handler; import android.os.Message; /** * 訪問WebService的工具類, * * @see http://blog.csdn.net/xiaanming * * @author xiaanming * */ public class WebServiceUtils { public static final String WEB_SERVER_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; // 含有3個執行緒的執行緒池 private static final ExecutorService executorService = Executors .newFixedThreadPool(3); // 名稱空間 private static final String NAMESPACE = "http://WebXml.com.cn/"; /** * * @param url * WebService伺服器地址 * @param methodName * WebService的呼叫方法名 * @param properties * WebService的引數 * @param webServiceCallBack * 回撥介面 */ public static void callWebService(String url, final String methodName, HashMap<String, String> properties, final WebServiceCallBack webServiceCallBack) { // 建立HttpTransportSE物件,傳遞WebService伺服器地址 final HttpTransportSE httpTransportSE = new HttpTransportSE(url); // 建立SoapObject物件 SoapObject soapObject = new SoapObject(NAMESPACE, methodName); // SoapObject新增引數 if (properties != null) { for (Iterator<Map.Entry<String, String>> it = properties.entrySet() .iterator(); it.hasNext();) { Map.Entry<String, String> entry = it.next(); soapObject.addProperty(entry.getKey(), entry.getValue()); } } // 例項化SoapSerializationEnvelope,傳入WebService的SOAP協議的版本號 final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); // 設定是否呼叫的是.Net開發的WebService soapEnvelope.setOutputSoapObject(soapObject); soapEnvelope.dotNet = true; httpTransportSE.debug = true; // 用於子執行緒與主執行緒通訊的Handler final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); // 將返回值回撥到callBack的引數中 webServiceCallBack.callBack((SoapObject) msg.obj); } }; // 開啟執行緒去訪問WebService executorService.submit(new Runnable() { @Override public void run() { SoapObject resultSoapObject = null; try { httpTransportSE.call(NAMESPACE + methodName, soapEnvelope); if (soapEnvelope.getResponse() != null) { // 獲取伺服器響應返回的SoapObject resultSoapObject = (SoapObject) soapEnvelope.bodyIn; } } catch (HttpResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } finally { // 將獲取的訊息利用Handler傳送到主執行緒 mHandler.sendMessage(mHandler.obtainMessage(0, resultSoapObject)); } } }); } /** * * * @author xiaanming * */ public interface WebServiceCallBack { public void callBack(SoapObject result); } }
我們通過呼叫裡面的callWebService(String url, final String methodName,HashMap<String, String> properties,final WebServiceCallBack webServiceCallBack)就可以來獲取我們想要的資料,現在講解下里面的實現思路
- 建立HttpTransportsSE物件。通過HttpTransportsSE類的構造方法可以指定WebService的WSDL文件的URL
- 建立SoapObject物件,裡面的引數分別是WebService的名稱空間和呼叫方法名
- 設定呼叫方法的引數值,如果沒有引數,就不設定,有引數的話呼叫SoapObject物件的addProperty(String name, Object value)方法將引數加入到SoapObject物件中
- 例項化SoapSerializationEnvelope,傳入WebService的SOAP協議的版本號,將上面的SoapObject物件通過setOutputSoapObject(Object soapObject)設定到裡面,並設定是否呼叫的是.Net開發的WebService和是否debug等資訊
- 因為涉及到網路操作,所以我們使用了執行緒池來非同步操作呼叫WebService介面,我們線上程中呼叫HttpTransportsSE物件的call(String soapAction, SoapEnvelope envelope)方法就能實現對WebService的呼叫,並且通過soapEnvelope.bodyIn獲取WebService返回的資訊,但是返回的資訊是在子執行緒中,我們需要利用Handler來實現子執行緒與主執行緒進行轉換,然後在Handler的handleMessage(Message msg)中將結果回撥到callBack的引數中,總體思路就是這個樣子,接下來我們來使用這個工具類吧
我們先用一個ListView來顯示所有的省份,然後點選每個省進去到市。市也用一個ListView來顯示,最後點選市用TextView來顯示獲取的WebService天氣情況,思路很簡單
用來顯示省份的佈局,裡面只有一個ListView
<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" >
<ListView
android:id="@+id/province_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:fadingEdge="none" >
</ListView>
</RelativeLayout>
接下來就是Activity的程式碼,先用工具類呼叫WebService方法,然後在回撥方法callBack(SoapObject result)中解析資料到一個List<String>中,在設定ListView的介面卡package com.example.webservicedemo;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.serialization.SoapObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
/**
* 顯示天氣省份的Activity
*
* @see http://blog.csdn.net/xiaanming
*
* @author xiaanming
*
*/
public class MainActivity extends Activity {
private List<String> provinceList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
final ListView mProvinceList = (ListView) findViewById(R.id.province_list);
//顯示進度條
ProgressDialogUtils.showProgressDialog(this, "資料載入中...");
//通過工具類呼叫WebService介面
WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportProvince", null, new WebServiceCallBack() {
//WebService介面返回的資料回撥到這個方法中
@Override
public void callBack(SoapObject result) {
//關閉進度條
ProgressDialogUtils.dismissProgressDialog();
if(result != null){
provinceList = parseSoapObject(result);
mProvinceList.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, provinceList));
}else{
Toast.makeText(MainActivity.this, "獲取WebService資料錯誤", Toast.LENGTH_SHORT).show();
}
}
});
mProvinceList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this, CityActivity.class);
intent.putExtra("province", provinceList.get(position));
startActivity(intent);
}
});
}
/**
* 解析SoapObject物件
* @param result
* @return
*/
private List<String> parseSoapObject(SoapObject result){
List<String> list = new ArrayList<String>();
SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportProvinceResult");
if(provinceSoapObject == null) {
return null;
}
for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){
list.add(provinceSoapObject.getProperty(i).toString());
}
return list;
}
}
點選省份進入該省份下面的市。也用一個ListView來顯示市的資料,佈局跟上面一樣,Activity裡面的程式碼也差不多相似,我就不過多說明了,直接看程式碼package com.example.webservicedemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.ksoap2.serialization.SoapObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
/**
* 顯示城市的Activity
*
* @see http://blog.csdn.net/xiaanming
*
* @author xiaanming
*
*/
public class CityActivity extends Activity {
private List<String> cityStringList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
final ListView mCityList = (ListView) findViewById(R.id.province_list);
//顯示進度條
ProgressDialogUtils.showProgressDialog(this, "資料載入中...");
//新增引數
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("byProvinceName", getIntent().getStringExtra("province"));
WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportCity", properties, new WebServiceCallBack() {
@Override
public void callBack(SoapObject result) {
ProgressDialogUtils.dismissProgressDialog();
if(result != null){
cityStringList = parseSoapObject(result);
mCityList.setAdapter(new ArrayAdapter<String>(CityActivity.this, android.R.layout.simple_list_item_1, cityStringList));
}else{
Toast.makeText(CityActivity.this, "獲取WebService資料錯誤", Toast.LENGTH_SHORT).show();
}
}
});
mCityList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(CityActivity.this, WeatherActivity.class);
intent.putExtra("city", cityStringList.get(position));
startActivity(intent);
}
});
}
/**
* 解析SoapObject物件
* @param result
* @return
*/
private List<String> parseSoapObject(SoapObject result){
List<String> list = new ArrayList<String>();
SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportCityResult");
for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){
String cityString = provinceSoapObject.getProperty(i).toString();
list.add(cityString.substring(0, cityString.indexOf("(")).trim());
}
return list;
}
}
接下來就是點選相對應的城市呼叫WebService介面來獲取該城市下面的天氣詳情啦,為了簡單起見,我用一個TextView來顯示天氣資訊,因為天氣資訊很多,一個螢幕顯示不完,所以我們考慮在外面加一個ScrollView來進行滾動<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/weather"
android:textColor="#336598"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Activity的程式碼就不做過多說明,跟上面的大同小異package com.example.webservicedemo;
import java.util.HashMap;
import org.ksoap2.serialization.SoapObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
/**
* 顯示天氣的Activity
*
* @see http://blog.csdn.net/xiaanming
*
* @author xiaanming
*
*/
public class WeatherActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather_layout);
init();
}
private void init() {
final TextView mTextWeather = (TextView) findViewById(R.id.weather);
ProgressDialogUtils.showProgressDialog(this, "資料載入中...");
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("theCityName", getIntent().getStringExtra("city"));
WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getWeatherbyCityName", properties, new WebServiceCallBack() {
@Override
public void callBack(SoapObject result) {
ProgressDialogUtils.dismissProgressDialog();
if(result != null){
SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
StringBuilder sb = new StringBuilder();
for(int i=0; i<detail.getPropertyCount(); i++){
sb.append(detail.getProperty(i)).append("\r\n");
}
mTextWeather.setText(sb.toString());
}else{
Toast.makeText(WeatherActivity.this, "獲取WebService資料錯誤", Toast.LENGTH_SHORT).show();
}
}
});
}
}
到這裡我們就完成了編碼工作,在執行程式之前我們需要在AndroidManifest.xml註冊Activity,以及新增訪問網路的許可權<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.webservicedemo.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CityActivity"/>
<activity android:name=".WeatherActivity"></activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
執行結果:省份,城市列表可以加上A-Z的排序功能,可以參考下Android實現ListView的A-Z字母排序和過濾搜尋功能,實現漢字轉成拼音,我這裡就不添加了,需要新增的朋友自行實現,好了,今天的講解到此結束,有疑問的朋友請在下面留言。