這個項目是基於webservice的,用ksoap2來解析網絡上的WebService的,我們先看做出的效果圖
其實也沒有很多技術難題,我們直接來看源碼再做說明吧
Android天氣預報源碼下載
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/2月/13日/Android開發教程:Android天氣預報/
[java]
- import java.util.ArrayList;
- import java.util.List;
-
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.HttpTransportSE;
-
- public class WebServiceUtil
- {
- // 定義Web Service的命名空間
- static final String SERVICE_NS = "http://WebXml.com.cn/";
- // 定義Web Service提供服務的URL
- static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
-
- /**
- * 獲得州,國內外省份和城市信息
- *
- * @return
- */
- public static List<String> getProvinceList()
- {
-
- // 需要調用的方法名(獲得本天氣預報Web Services支持的洲、國內外省份和城市信息)
- String methodName = "getRegionProvince";
- // 創建HttpTransportSE傳輸對象
- HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
-
- httpTranstation.debug = true;
- // 使用SOAP1.1協議創建Envelop對象
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
- SoapEnvelope.VER11);
- // 實例化SoapObject對象
- SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
- envelope.bodyOut = soapObject;
- // 設置與.Net提供的Web Service保持較好的兼容性
- envelope.dotNet = true;
- try
- {
- // 調用Web Service
- httpTranstation.call(SERVICE_NS + methodName, envelope);
- if (envelope.getResponse() != null)
- {
- // 獲取服務器響應返回的SOAP消息
- SoapObject result = (SoapObject) envelope.bodyIn;
- SoapObject detail = (SoapObject) result.getProperty(methodName
- + "Result");
- // 解析服務器響應的SOAP消息。
- return parseProvinceOrCity(detail);
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
-
- return null;
- }
-
- /**
- * 根據省份獲取城市列表
- *
- * @param province
- * @return
- */
- public static List<String> getCityListByProvince(String province)
- {
-
- // 需要調用的方法名(獲得本天氣預報Web Services支持的城市信息,根據省份查詢城市集合:帶參數)
- String methodName = "getSupportCityString";
-
- HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
- httpTranstation.debug = true;
-
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
- SoapEnvelope.VER11);
-
- SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
- soapObject.addProperty("theRegionCode", province);
- envelope.bodyOut = soapObject;
- envelope.dotNet = true;
- try
- {
- // 調用Web Service
- httpTranstation.call(SERVICE_NS + methodName, envelope);
- if (envelope.getResponse() != null)
- {
- // 獲取服務器響應返回的SOAP消息
- SoapObject result = (SoapObject) envelope.bodyIn;
- SoapObject detail = (SoapObject) result.getProperty(methodName
- + "Result");
- // 解析服務器響應的SOAP消息。
- return parseProvinceOrCity(detail);
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
-
- return null;
-
- }
-
- private static List<String> parseProvinceOrCity(SoapObject detail)
- {
- ArrayList<String> result = new ArrayList<String>();
- for (int i = 0; i < detail.getPropertyCount(); i++)
- {
- String str = detail.getProperty(i).toString();
- // 解析出每個省份
- result.add(str.split(",")[0]);
- }
- return result;
- }
-
- public static SoapObject getWeatherByCity(String cityName)
- {
-
- // 根據城市或地區名稱查詢獲得未來三天內天氣情況、現在的天氣實況、天氣和生活指數
- String methodName = "getWeather";
-
- HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
- httpTranstation.debug = true;
-
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
- SoapEnvelope.VER11);
- SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
- soapObject.addProperty("theCityCode", cityName);
- envelope.bodyOut = soapObject;
- envelope.dotNet = true;
-
- try
- {
- // 調用Web Service
- httpTranstation.call(SERVICE_NS + methodName, envelope);
- if (envelope.getResponse() != null)
- {
- // 獲取服務器響應返回的SOAP消息
- SoapObject result = (SoapObject) envelope.bodyIn;
- SoapObject detail = (SoapObject) result.getProperty(methodName
- + "Result");
- // 解析服務器響應的SOAP消息。
- return detail;
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
-
- return null;
- }
-
- }