Android 获取Google Weather API 并通过Xml和JSON解析数据
2011-10-19 21:45
1066 查看
设计如下:通过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/)
结构如下:
以下是效果图:
(国家列表)
(城市列表)
(天气情况)
主要代码(代码不做多余解释自己看吧):
一、获取数据
view
plain
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
/**
* 得到数据
* @param args
* @return
*/
public final static InputStream getStream(String args) {
InputStream stream = null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(args);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
stream = entity.getContent();
}
return stream;
} catch (Exception e) {
e.printStackTrace();
return stream;
}
}
二、解析天气
view
plain
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
/**
* 通过解析xml数据得到天气信息
* @param args
* @return
*/
public static WeatherBean getCurrentWeather(String args){
Document document = null;
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = builder.parse(new InputSource(getStream(args)));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 当天天气
WeatherBean weather= new WeatherBean();
NodeList nodeList = document.getElementsByTagName("current_conditions").item(0).getChildNodes(); // 当前天气
String condition = nodeList.item(0).getAttributes().item(0).getNodeValue(); // 天气情况
String tempF = nodeList.item(1).getAttributes().item(0).getNodeValue(); // 华氏度
String tempC = nodeList.item(2).getAttributes().item(0).getNodeValue(); // 摄氏度
String humidity = nodeList.item(3).getAttributes().item(0).getNodeValue(); // 湿度
String imgUrl = nodeList.item(4).getAttributes().item(0).getNodeValue(); // 天气图片
String windCondition = 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);
return weather;
}
三、Google返回的Json数据,用Json解析城市
view
plain
/**
* 通过Android 提供的Json方式解析城市信息
* @param countryCode
* @return
*/
public static List<CityBean> getCityInfos(String countryCode) {
List<CityBean> cityList = new ArrayList<CityBean>();
//
StringBuilder sBuilder = new StringBuilder();
BufferedReader bReader = new BufferedReader(new InputStreamReader(getStream(countryCode)));
try {
for (String s = bReader.readLine(); s != null; s = bReader.readLine()) {
sBuilder.append(s);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject jsonObject = new JSONObject(sBuilder.toString());
JSONArray jsonArray = jsonObject.getJSONArray("cities");
for (int i = 0; i < jsonArray.length(); i++) {
CityBean cityBean = new CityBean();
JSONObject jsonObj = (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 (JSONException e) {
e.printStackTrace();
}
return cityList;
}
我这里只是通过经纬度查询城市天气预报。也可以通过其他的方式,具体的请看上面那个链接。
Demo下载地址:http://download.csdn.net/source/3339328
知识点:
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/)
结构如下:
以下是效果图:
(国家列表)
(城市列表)
(天气情况)
主要代码(代码不做多余解释自己看吧):
一、获取数据
view
plain
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
/**
* 得到数据
* @param args
* @return
*/
public final static InputStream getStream(String args) {
InputStream stream = null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(args);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
stream = entity.getContent();
}
return stream;
} catch (Exception e) {
e.printStackTrace();
return stream;
}
}
二、解析天气
view
plain
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
/**
* 通过解析xml数据得到天气信息
* @param args
* @return
*/
public static WeatherBean getCurrentWeather(String args){
Document document = null;
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = builder.parse(new InputSource(getStream(args)));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 当天天气
WeatherBean weather= new WeatherBean();
NodeList nodeList = document.getElementsByTagName("current_conditions").item(0).getChildNodes(); // 当前天气
String condition = nodeList.item(0).getAttributes().item(0).getNodeValue(); // 天气情况
String tempF = nodeList.item(1).getAttributes().item(0).getNodeValue(); // 华氏度
String tempC = nodeList.item(2).getAttributes().item(0).getNodeValue(); // 摄氏度
String humidity = nodeList.item(3).getAttributes().item(0).getNodeValue(); // 湿度
String imgUrl = nodeList.item(4).getAttributes().item(0).getNodeValue(); // 天气图片
String windCondition = 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);
return weather;
}
三、Google返回的Json数据,用Json解析城市
view
plain
/**
* 通过Android 提供的Json方式解析城市信息
* @param countryCode
* @return
*/
public static List<CityBean> getCityInfos(String countryCode) {
List<CityBean> cityList = new ArrayList<CityBean>();
//
StringBuilder sBuilder = new StringBuilder();
BufferedReader bReader = new BufferedReader(new InputStreamReader(getStream(countryCode)));
try {
for (String s = bReader.readLine(); s != null; s = bReader.readLine()) {
sBuilder.append(s);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject jsonObject = new JSONObject(sBuilder.toString());
JSONArray jsonArray = jsonObject.getJSONArray("cities");
for (int i = 0; i < jsonArray.length(); i++) {
CityBean cityBean = new CityBean();
JSONObject jsonObj = (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 (JSONException e) {
e.printStackTrace();
}
return cityList;
}
我这里只是通过经纬度查询城市天气预报。也可以通过其他的方式,具体的请看上面那个链接。
Demo下载地址:http://download.csdn.net/source/3339328
相关文章推荐
- Android 获取Google Weather API 并通过Xml和JSON解析数据
- 【Android应用开发】-(17)获取Google天气预报通过Xml和JSON解析数据
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android:通过url向网络端获取json格式数据并解析显示
- android解析xml和json数据(获取本地服务器数据)
- android通过httpClient请求获取JSON数据并且解析
- Android 神兵利器之通过解析网页获取到的API数据合集,可拿来就用
- Android 神兵利器之通过解析网页获取到的API数据合集,可拿来就用
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- asp通过json调用webservice接口,并获取返回的xml数据及解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- Android手机号码归属地的查询(使用聚合数据API,获取JSON数据并解析)
- android通过httpClient请求获取JSON数据并且解析
- C#通过WebService获取JSON数据并快速解析(百度地图坐标转换API为例)
- Android - 获取网络数据,Bitmap,ANR,主线程模型,消息机制,UserAgent,乱码问题,xml生成与解析,JSON解析,MVC,SmartImageView
- Android客户端从服务端获取数据JSON解析
- 在 Android 应用程序中使用 Internet 数据 解析 XML、JSON 和 protocol buffers 数据