设计如下:通过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/)
结构如下:
以下是效果图:
(国家列表)
(城市列表)
(天气情况)
主要代码(代码不做多余解释自己看吧):
一、获取数据
- /**
- *得到数据
- *@paramargs
- *@return
- */
- publicfinalstaticInputStreamgetStream(Stringargs){
- InputStreamstream=null;
- DefaultHttpClientclient=newDefaultHttpClient();
- HttpGetget=newHttpGet(args);
- try{
- HttpResponseresponse=client.execute(get);
- if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
- HttpEntityentity=response.getEntity();
- stream=entity.getContent();
- }
- returnstream;
- }catch(Exceptione){
- e.printStackTrace();
- returnstream;
- }
- }
二、解析天气
- /**
- *通过解析xml数据得到天气信息
- *@paramargs
- *@return
- */
- publicstaticWeatherBeangetCurrentWeather(Stringargs){
- Documentdocument=null;
- try{
- DocumentBuilderbuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
- document=builder.parse(newInputSource(getStream(args)));
- }catch(ParserConfigurationExceptione){
- e.printStackTrace();
- }catch(FactoryConfigurationErrore){
- e.printStackTrace();
- }catch(SAXExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- //当天天气
- WeatherBeanweather=newWeatherBean();
- NodeListnodeList=document.getElementsByTagName("current_conditions").item(0).getChildNodes();//当前天气
- Stringcondition=nodeList.item(0).getAttributes().item(0).getNodeValue();//天气情况
- StringtempF=nodeList.item(1).getAttributes().item(0).getNodeValue();//华氏度
- StringtempC=nodeList.item(2).getAttributes().item(0).getNodeValue();//摄氏度
- Stringhumidity=nodeList.item(3).getAttributes().item(0).getNodeValue();//湿度
- StringimgUrl=nodeList.item(4).getAttributes().item(0).getNodeValue();//天气图片
- StringwindCondition=nodeList.item(5).getAttributes().item(0).getNodeValue();//风速描述
- weather.setCondition(condition);
- weather.setTempF(Integer.parseInt(tempF));
- weather.setTempC(Integer.parseInt(tempC));
- weather.setHumidity(humidity);
- //weather.setIcon(reDrawable(imgUrl));//解析图片
- weather.setWindCondition(windCondition);
- returnweather;
- }
三、Google返回的Json数据,用Json解析城市
- /**
- *通过Android提供的Json方式解析城市信息
- *@paramcountryCode
- *@return
- */
- publicstaticList<CityBean>getCityInfos(StringcountryCode){
- List<CityBean>cityList=newArrayList<CityBean>();
- //
- StringBuildersBuilder=newStringBuilder();
- BufferedReaderbReader=newBufferedReader(newInputStreamReader(getStream(countryCode)));
- try{
- for(Strings=bReader.readLine();s!=null;s=bReader.readLine()){
- sBuilder.append(s);
- }
- }catch(IOExceptione){
- e.printStackTrace();
- }
- try{
- JSONObjectjsonObject=newJSONObject(sBuilder.toString());
- JSONArrayjsonArray=jsonObject.getJSONArray("cities");
- for(inti=0;i<jsonArray.length();i++){
- CityBeancityBean=newCityBean();
- JSONObjectjsonObj=(JSONObject)jsonArray.opt(i);
- cityBean.setCityName(jsonObj.getString("name"));
- cityBean.setLat(jsonObj.getLong("lat"));
- cityBean.setLon(jsonObj.getLong("lon"));
- cityList.add(cityBean);
- Log.i(TAG,"name="+jsonObj.getString("name")+";lat="+jsonObj.getLong("lat")+";lon="+jsonObj.getLong("lon"));
- }
- }catch(JSONExceptione){
- e.printStackTrace();
- }
- returncityList;
- }
我这里只是通过经纬度查询城市天气预报。也可以通过其他的方式,具体的请看上面那个链接。