最近在做一個天氣預報的Widget,通過google提供的api可以查詢全世界的天氣情況,這篇文章主要講述如何通過Android的JSON擷取城市的經緯度,程式很簡單。稍後我將demo供來此部落格的朋友。廢話少說,且看下文:
設計如下:通過JsonDemoActivity輸入國家簡稱,跳轉到CityListActivity(用來顯示城市列表),點擊需要查詢城市返回天氣資訊。在JsonDemoActivity顯示天氣資訊,Utils是解析天氣和城市的主要工具類。
知識點:
1、多個Activity之間傳遞資料(一般Activity之間用來傳遞的是基本的資料類型,比如說String,int,boolean等),其中有個方法,可以用來傳遞對象,我就是講城市和天氣資訊寫成相應的JavaBean,用來傳遞的;
2、Json資料解析,擷取城市;
3、解析Xml資料,擷取天氣;
4、部分Google API的講解;
5、解析圖片。
(關於google wearher api 的說明在:http://tsov.net/weather-queries-using-the-google-weather-api/)
結構如下:
以下是:
(國家列表)
(城市列表)
(天氣情況)
主要代碼(代碼不做多餘解釋自己看吧):
一、擷取資料
/**<br /> * 得到資料<br /> * @param args<br /> * @return<br /> */<br />public final static InputStream getStream(String args) {<br />InputStream stream = null;<br />DefaultHttpClient client = new DefaultHttpClient();<br />HttpGet get = new HttpGet(args);<br />try {<br />HttpResponse response = client.execute(get);<br />if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {<br />HttpEntity entity = response.getEntity();<br />stream = entity.getContent();<br />}<br />return stream;<br />} catch (Exception e) {<br />e.printStackTrace();<br />return stream;<br />}</p><p>}
二、解析天氣
/**<br /> * 通過解析xml資料得到天氣資訊<br /> * @param args<br /> * @return<br /> */<br />public static WeatherBean getCurrentWeather(String args){</p><p>Document document = null;<br />try {<br />DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();<br />document = builder.parse(new InputSource(getStream(args)));<br />} catch (ParserConfigurationException e) {<br />e.printStackTrace();<br />} catch (FactoryConfigurationError e) {<br />e.printStackTrace();<br />} catch (SAXException e) {<br />e.printStackTrace();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}</p><p>// 當天天氣<br />WeatherBean weather= new WeatherBean();</p><p>NodeList nodeList = document.getElementsByTagName("current_conditions").item(0).getChildNodes(); // 當前天氣</p><p>String condition = nodeList.item(0).getAttributes().item(0).getNodeValue(); // 天氣情況<br />String tempF = nodeList.item(1).getAttributes().item(0).getNodeValue(); // 華氏度<br />String tempC = nodeList.item(2).getAttributes().item(0).getNodeValue(); // 攝氏度<br />String humidity = nodeList.item(3).getAttributes().item(0).getNodeValue(); // 濕度<br />String imgUrl = nodeList.item(4).getAttributes().item(0).getNodeValue(); // 天氣圖片<br />String windCondition = nodeList.item(5).getAttributes().item(0).getNodeValue(); // 風速描述</p><p>weather.setCondition(condition);<br />weather.setTempF(Integer.parseInt(tempF));<br />weather.setTempC(Integer.parseInt(tempC));<br />weather.setHumidity(humidity);<br />//weather.setIcon(reDrawable(imgUrl)); // 解析圖片<br />weather.setWindCondition(windCondition);<br />return weather;</p><p>}
三、Google返回的Json資料,用Json解析城市
/**<br /> * 通過Android 提供的Json方式解析城市資訊<br /> * @param countryCode<br /> * @return<br /> */<br />public static List<CityBean> getCityInfos(String countryCode) {<br />List<CityBean> cityList = new ArrayList<CityBean>();<br />//<br />StringBuilder sBuilder = new StringBuilder();<br />BufferedReader bReader = new BufferedReader(new InputStreamReader(getStream(countryCode)));<br />try {<br />for (String s = bReader.readLine(); s != null; s = bReader.readLine()) {<br />sBuilder.append(s);<br />}<br />} catch (IOException e) {<br />e.printStackTrace();<br />} </p><p>try {<br />JSONObject jsonObject = new JSONObject(sBuilder.toString());<br />JSONArray jsonArray = jsonObject.getJSONArray("cities"); </p><p>for (int i = 0; i < jsonArray.length(); i++) {<br />CityBean cityBean = new CityBean();<br />JSONObject jsonObj = (JSONObject) jsonArray.opt(i);<br />cityBean.setCityName(jsonObj.getString("name"));<br />cityBean.setLat(jsonObj.getLong("lat"));<br />cityBean.setLon(jsonObj.getLong("lon"));<br />cityList.add(cityBean);<br />Log.i(TAG, "name="+jsonObj.getString("name")+";lat="+jsonObj.getLong("lat")+";lon="+jsonObj.getLong("lon"));<br />}<br />} catch (JSONException e) {<br />e.printStackTrace();<br />}</p><p>return cityList;<br />}<br />
我這裡只是通過經緯度查詢城市天氣預報。也可以通過其他的方式,具體的請看上面那個連結。
Demo:http://download.csdn.net/source/3339328