新聞中心

EEPW首頁(yè) > 模擬技術(shù) > 設(shè)計(jì)應(yīng)用 > Android手機(jī)天氣預(yù)報(bào)系統(tǒng)設(shè)計(jì)及實(shí)現(xiàn)

Android手機(jī)天氣預(yù)報(bào)系統(tǒng)設(shè)計(jì)及實(shí)現(xiàn)

作者: 時(shí)間:2012-03-22 來(lái)源:網(wǎng)絡(luò) 收藏
有一個(gè)項(xiàng)目ksoap2-android(http://code.google.com/p/ksoap2-android/)可用在 平臺(tái)上實(shí)現(xiàn)輕量級(jí)的SOAP 庫(kù),即使用ksoap2-android 的API 來(lái)調(diào)用遠(yuǎn)端Web Service 的服務(wù)。

  因此需要在CitiesWeatherForecast.java 的onCreate()中進(jìn)行數(shù)據(jù)獲取的初始化工作。通過(guò)定義一個(gè)方法public ListgetAllCitiesNAMEs()可獲得中國(guó)城市名稱(chēng)列表。也就是說(shuō),采用它提供的獲得全球各個(gè)國(guó)家城市的方法GetCitiesByCountry,通過(guò)http://www.webservicex.net/globalweather.asmx 上的WebService 可提供服務(wù)。譬如,通過(guò)Constant.java 中的public class Constant{},設(shè)置SOAP Action 要調(diào)用的方法名、命名空間以及Web Service URL值,其源代碼為:

  public static final String SOAP_ACTION=

  "http://www.webserviceX.NET/GetCitiesByCountry";

  public static final String METHOD_NAME=

  "GetCitiesByCountry";

  public static final String SOAP_ACTION2=

  "http://www.webserviceX.NET/GetWeather";

  public static final String METHOD_NAME2="GetWeather";

  public static final String NAMESPACE=

  "http://www.webserviceX.NET";

  public static final String URL=

  http://www.webservicex.net/globalweather.asmx;

  然后在WebServiceCaller.java 中實(shí)例化一個(gè)SoapSerializationEnvelope對(duì)象,設(shè)置SoapObject 的命名空間、方法名、參數(shù)等;并通過(guò)實(shí)例化一個(gè)HttpTransport 對(duì)象來(lái)調(diào)用WebService,并獲得xml 字符串?dāng)?shù)據(jù),其代碼段如下:

  HttpTransport androidHttpTransport=

  new AndroidHttpTransport (Constant.URL);

  try {

  androidHttpTransport.call(soapAction, envelope);

  Object result=envelope.getResponse();

  xmlStr=result.toString();

  } catch(Exception e) {

  e.printStackTrace();

  }

 ?、?解析數(shù)據(jù)列表

  Android 操作系統(tǒng)對(duì)xml 字符串?dāng)?shù)據(jù)的操作功能很強(qiáng),提供了dom、sax 以及xmlpul 3 種方式。Android SDK 提供了android.sax 包以方便SAX Handler 的開(kāi)發(fā),可用來(lái)解析xml 結(jié)果字符串。在本設(shè)計(jì)實(shí)例的CitiesWeatherForecast.java 文件中,解析xml 數(shù)據(jù)時(shí),先通過(guò)RootElement root=new RootElement("NewDataSet")獲得xml 數(shù)據(jù)的根節(jié)點(diǎn);然后再尋找其子節(jié)點(diǎn)直到找到City 子節(jié)點(diǎn),并設(shè)置一個(gè)監(jiān)聽(tīng)器setEndTextElementListener()來(lái)獲得City 子節(jié)點(diǎn)的值;最后使用org.xml.sax 的SAXParser 來(lái)解析xml 數(shù)據(jù),把數(shù)據(jù)存入List并返回:

  SAXParserFactory factory=SAXParserFactory.newInstance();

  SAXParser parser=factory.newSAXParser();

  XMLReader xmlreader=parser.getXMLReader();

  xmlreader.setContentHandler(root.getContentHandler());

  InputSource is=new InputSource

  (new StringBufferInputStream(xmlStr));

  xmlreader.parse(is);

 ?、?在UI上呈現(xiàn)城市列表數(shù)據(jù)

  當(dāng)獲得List類(lèi)型的數(shù)據(jù)之后,就可以把它綁定到main.xml 定義的ListView 組件上,然后使用ListActivity 呈現(xiàn)列表數(shù)據(jù)。在CitiesWeatherForecast.java 中的代碼段為:

  List cityList=getAllCitiesNames();

  setListAdapter(new ArrayAdapter(this,

  android.R.layout.simple_list_item_1, cityList));

  即使用方法setListAdapter()把一個(gè)ListActivity 填充進(jìn)去。

  2.3 城市天氣實(shí)況預(yù)報(bào)系統(tǒng)的調(diào)試

  城市天氣實(shí)況預(yù)報(bào)系統(tǒng)的測(cè)試結(jié)果如下。在Eclipse 的Package Explorer 窗口中用鼠標(biāo)右鍵選擇CitiesWeatherForecast工程名,在彈出的窗口中選擇"Run As"→"Android Application"安裝該應(yīng)用程序到Android 模擬器并啟動(dòng)它。然后單擊所要查詢(xún)的城市名稱(chēng)列表項(xiàng),稍等片刻便會(huì)顯示出該城市的天氣實(shí)況信息。也可以在文本框中直接輸入所要查詢(xún)的城市名稱(chēng),單擊"Search"同樣會(huì)獲得相應(yīng)城市的信息。

  3 結(jié)束語(yǔ)

  本文在介紹基于Android 平臺(tái)的應(yīng)用程序設(shè)計(jì)原理的基礎(chǔ)上,提出了Android 用戶(hù)界面設(shè)計(jì)、獲取并解析城市列表數(shù)據(jù)的一種方法,給出了在用戶(hù)界面上呈現(xiàn)數(shù)據(jù)的原理與設(shè)計(jì)過(guò)程,最后通過(guò)模擬器進(jìn)行了應(yīng)用程序的調(diào)試。當(dāng)然,基于Android 平臺(tái)的開(kāi)發(fā)技術(shù)還需要進(jìn)一步完善,需要在日后的工作中不斷探索、研究,以建立實(shí)用的城市天氣實(shí)況預(yù)報(bào)系統(tǒng)。


上一頁(yè) 1 2 下一頁(yè)

評(píng)論


相關(guān)推薦

技術(shù)專(zhuān)區(qū)

關(guān)閉