Android 获取Google Weather API 并通过Xml和JSON解析数据

 最近在做一个天气预报的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/

   

结构如下:

以下是效果图:

(国家列表)

 

 (城市列表)

 

 (天气情况)

 

主要代码(代码不做多余解释自己看吧):

 

一、获取数据

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. /** 
  2.      * 得到数据 
  3.      * @param args 
  4.      * @return 
  5.      */  
  6.     public final static InputStream getStream(String args) {  
  7.         InputStream stream = null;  
  8.         DefaultHttpClient client = new DefaultHttpClient();  
  9.         HttpGet get = new HttpGet(args);  
  10.         try {  
  11.             HttpResponse response = client.execute(get);  
  12.             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  13.                 HttpEntity entity = response.getEntity();  
  14.                 stream = entity.getContent();  
  15.             }  
  16.             return stream;  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.             return stream;  
  20.         }  
  21.           
  22.     }  

二、解析天气

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1.     /** 
  2.      * 通过解析xml数据得到天气信息 
  3.      * @param args 
  4.      * @return 
  5.      */  
  6.     public static WeatherBean getCurrentWeather(String args){  
  7.           
  8.           
  9.         Document document = null;  
  10.         try {  
  11.             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();  
  12.             document = builder.parse(new InputSource(getStream(args)));  
  13.         } catch (ParserConfigurationException e) {  
  14.             e.printStackTrace();  
  15.         } catch (FactoryConfigurationError e) {  
  16.             e.printStackTrace();  
  17.         } catch (SAXException e) {  
  18.             e.printStackTrace();  
  19.         } catch (IOException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.           
  23.         // 当天天气   
  24.         WeatherBean weather= new WeatherBean();  
  25.           
  26.         NodeList nodeList = document.getElementsByTagName("current_conditions").item(0).getChildNodes();    // 当前天气   
  27.           
  28.         String condition = nodeList.item(0).getAttributes().item(0).getNodeValue();     // 天气情况   
  29.         String tempF = nodeList.item(1).getAttributes().item(0).getNodeValue();         // 华氏度   
  30.         String tempC = nodeList.item(2).getAttributes().item(0).getNodeValue();         // 摄氏度   
  31.         String humidity = nodeList.item(3).getAttributes().item(0).getNodeValue();      // 湿度   
  32.         String imgUrl = nodeList.item(4).getAttributes().item(0).getNodeValue();        // 天气图片   
  33.         String windCondition = nodeList.item(5).getAttributes().item(0).getNodeValue(); // 风速描述   
  34.           
  35.         weather.setCondition(condition);  
  36.         weather.setTempF(Integer.parseInt(tempF));  
  37.         weather.setTempC(Integer.parseInt(tempC));  
  38.         weather.setHumidity(humidity);  
  39. //      weather.setIcon(reDrawable(imgUrl));  // 解析图片   
  40.         weather.setWindCondition(windCondition);  
  41.         return weather;  
  42.           
  43.     }  

三、Google返回的Json数据,用Json解析城市

 

  1. /** 
  2.  * 通过Android 提供的Json方式解析城市信息 
  3.  * @param countryCode 
  4.  * @return 
  5.  */  
  6. public static List<CityBean> getCityInfos(String countryCode) {  
  7.     List<CityBean> cityList = new ArrayList<CityBean>();  
  8.     //   
  9.     StringBuilder sBuilder = new StringBuilder();  
  10.     BufferedReader bReader = new BufferedReader(new InputStreamReader(getStream(countryCode)));   
  11.     try {  
  12.         for (String s = bReader.readLine(); s != null; s = bReader.readLine()) {   
  13.             sBuilder.append(s);   
  14.         }  
  15.     } catch (IOException e) {  
  16.         e.printStackTrace();  
  17.     }   
  18.       
  19.     try {  
  20.         JSONObject jsonObject = new JSONObject(sBuilder.toString());   
  21.         JSONArray jsonArray = jsonObject.getJSONArray("cities");   
  22.           
  23.         for (int i = 0; i < jsonArray.length(); i++) {  
  24.             CityBean cityBean = new CityBean();  
  25.             JSONObject jsonObj = (JSONObject) jsonArray.opt(i);  
  26.             cityBean.setCityName(jsonObj.getString("name"));  
  27.             cityBean.setLat(jsonObj.getLong("lat"));  
  28.             cityBean.setLon(jsonObj.getLong("lon"));  
  29.             cityList.add(cityBean);  
  30.             Log.i(TAG, "name="+jsonObj.getString("name")+";lat="+jsonObj.getLong("lat")+";lon="+jsonObj.getLong("lon"));  
  31.         }  
  32.     } catch (JSONException e) {  
  33.         e.printStackTrace();  
  34.     }  
  35.   
  36.     return cityList;  
  37. }  

 

我这里只是通过经纬度查询城市天气预报。也可以通过其他的方式,具体的请看上面那个链接。

 

Demo下载地址:http://download.csdn.net/source/3339328

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值