相信你們在日常的開發中,對網絡的操做用到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,能夠從http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2進行下載,將jar包加入到libs目錄下就好了,接下來帶你們來調用WebService接口java
首先咱們新建一個工程,取名WebServiceDemo,咱們從http://www.webxml.com.cn/zh_cn/web_services.aspx來獲取WebService接口,這裏面有一些免費的WebService接口,咱們就用裏面的天氣接口吧http://www.webxml.com.cn/WebServices/WeatherWebService.asmxandroid
咱們新建一個WebService的工具類,用於對WebService接口的調用,之後遇到調用WebService直接拷貝來用就好了web
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)就能夠來獲取咱們想要的數據,如今講解下里面的實現思路服務器
咱們先用一個ListView來顯示全部的省份,而後點擊每一個省進去到市。市也用一個ListView來顯示,最後點擊市用TextView來顯示獲取的WebService天氣狀況,思路很簡單網絡
用來顯示省份的佈局,裏面只有一個ListViewapp
<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裏面的代碼也差很少類似,我就不過多說明了,直接看代碼ide
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字母排序和過濾搜索功能,實現漢字轉成拼音,我這裏就不添加了,須要添加的朋友自行實現,好了,今天的講解到此結束,有疑問的朋友請在下面留言。