最近在做一个小东西。需要使用到天气预报。之前查了查了很多有关于这个的代码。但是最近才看懂。
首先,需要找到一个可用的天气API接口。之前 一直尝试的是中国天气预报网的。http://www.weather.com.cn/weather1d/101120101.shtml
但是使用json一直解析失败。不知道是怎么回事。
后来发现可以使用百度车联网提供的天气API接口。http://developer.baidu.com/map/index.php?title=car/api/weather
这里面的给的接口实例是这样的:
但是如果我们就这样写的话,没有办法得到数据。是因为还得将我们的mcode加在后面。如下所示:
"http://api.map.baidu.com/telematics/v3/weather?location="+ URLEncoder.encode(城市名称, "UTF-8")+"&output=json&ak=你的百度ak&mcode=你的mcode;
百度ak申请地址:
http://lbsyun.baidu.com/apiconsole/key
接下来就可以开始进行json数据的获取。
private String getRequst(String uri) {
httpGet = new HttpGet(uri);
String result = "";
try {
httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = httpResponse.getEntity();
result= EntityUtils.toString(entity);
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
此时返回的result就是网页以json的形式返回的数据。
拿到的json数据格式如下:
{"error":0,"status":"success","date":"2015-03-06",
"results":[{"currentCity":"济南","pm25":"96",
"index":[
{"title":"穿衣","zs":"较冷","tipt":"穿衣指数","des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},
{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},
{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,风稍大,但温度适宜,是个好天气哦。适宜旅游,您可以尽情地享受大自然的无限风光。"},
{"title":"感冒","zs":"少发","tipt":"感冒指数","des":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。"},
{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,但考虑风力较强且气温较低,推荐您进行室内运动,若在户外运动注意防风并适当增减衣物。"},
{"title":"紫外线强度","zs":"中等","tipt":"紫外线强度指数","des":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}],
"weather_data":[
{"date":"周五 03月06日 (实时:6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"南风3-4级","temperature":"5℃"},
{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"南风3-4级","temperature":"15 ~ 8℃"},
{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"北风3-4级","temperature":"14 ~ -1℃"},
{"date":"周一","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"北风3-4级","temperature":"7 ~ -2℃"}]}]
}
此时对json数据进行解析。
通过观察,发现我们拿到的数据是以键值对的形式出现的。并且在部分内容中包含了数组,而那些才是我们需要的内容。所以先通过JSONObject拿到对象,然后再得到其中的数组。
public void parseString(String jsonText) {
try {
JSONObject root;
JSONObject jsonObject = null;
root = new JSONObject(jsonText);
JSONArray array1 = root.getJSONArray("results");
Log.v(TAG, TAG+" lx array1 length is "+array1.length());
if(array1.length()>=1) {
jsonObject = array1.getJSONObject(array1.length()-1);
JSONArray array2 = jsonObject.getJSONArray("weather_data");
if(array2.length()>=1) {
JSONObject lan = array2.getJSONObject(0);
date = lan.getString("date");
weather = lan.getString("weather");
temperature = lan.getString("temperature");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}