写项目苦于没有天气数据,从各大开放的API请求数据的话太贵,没办法只能给项目经理说,他二话不说,直接暴力的从中国天气网上爬数据(牛的一批啊),默默崇拜中!!!
先上中国天气网的Uri: http://www.weather.com.cn/
手机查天气
下面就附上代码
public class WeatherUtils {
private final static int timeout = 3 * 1000;
private String url = "http://m.weather.com.cn/d/town/index?lat=${lat}&lon=${lon}";
/**
* 获取天气更新的信息
*
* @param doc
* @return {"time":"11:10更新","address":"兰州市保安服务总公司经济适用房东北","weather":"城关天气","city":"兰州市| 城关区"}
*/
private Map<String, Object> getInfo(Document doc) {
Map<String, Object> map = new HashMap<String, Object>();
Element wrap = doc.getElementsByClass("wrap").get(0);
Element city = wrap.getElementsByTag("h2").get(0);// 大的地址:兰州市|城关区
Element address = wrap.getElementsByClass("address").get(0);//详细地址:兰州市保安服务总公司经济适用房东北
Element weather = wrap.getElementsByClass("xian").get(0);//城关天气
Element time = wrap.getElementsByTag("time").get(0);//更新时间
// map.put("city", city.text());
// map.put("address", address.text());
// map.put("weather", weather.text());
map.put("time", time.text());
// map.put("cityCode", weather.attr("href").replaceAll("\\/mweather\\/(\\d+)\\.shtml", "$1"));
return map;
}
private Map<String, Object> getCurrentWeather(Document doc) {
Map<String, Object> map = new HashMap<String, Object>();
Element midd = doc.getElementsByClass("n_wd").get(0);
Element currentTemp = midd.getElementsByTag("h1").get(0)
.getElementsByTag("span").get(0);//当前温度
Element weather = midd.getElementsByTag("h1").get(0)
.getElementsByTag("em").get(0);;//当前的天气 :阴
Element wind = midd.getElementsByTag("h2").get(0)
.getElementsByClass("flfx").get(0);//风向
Element humidity = midd.getElementsByTag("h2").get(0)
.getElementsByClass("xdsd").get(0);//相对湿度
map.put("temp", currentTemp.text());
map.put("weather", weather.text());
map.put("wind", wind.text());
map.put("humidity", humidity.text());
Element swiperwrapper1 = doc.getElementsByClass("swiper-wrapper1").get(0)
.getElementsByClass("swiper-slide").get(0);
Element i = swiperwrapper1.getElementsByTag("i").get(0);
Element div = swiperwrapper1.getElementsByClass("tempLi").get(0);
map.put("img", i.attr("class").replace("svnicon housr_icons ", ""));
map.put("current", div.text());
return map;
}
private Object getWeather(String cityCode, String referer) throws IOException {
String url = "http://d1.weather.com.cn/dingzhi/" + cityCode + ".html";
Connection conn = Jsoup.connect(url);
conn.header("Referer", referer);
Document doc = conn.get();
String html = doc.html();
String data = html.replaceAll("[\\r|\\n]", "")
.replaceAll(".*?(\\{.*?);.*", "$1");
WeatherObject object = new Gson().fromJson(data, WeatherObject.class);
return object.getWeatherinfo();
}
private Map<String, Object> getShzs(Document doc) {
Map<String, Object> map = new HashMap<String, Object>();
Elements elements = doc.getElementsByClass("shzs").get(0)
.getElementsByClass("swiper-slide");
for (Element element : elements) {
map.put(element.getElementsByTag("span").get(0).text(),
element.getElementsByTag("b").get(0).text());
}
return map;
}
public String query(String url) throws IOException {
Connection conn = Jsoup.connect(url);
conn.timeout(timeout);
Document doc = conn.get();
Map<String, Object> all = new HashMap<String, Object>();
Map<String, Object> info = this.getInfo(doc);
Map<String, Object> weather = this.getCurrentWeather(doc);
// Map<String, Object> shzs = this.getShzs(doc);
//Object weatherinfo = this.getWeather((String)info.get("cityCode"),url);
all.put("info", info);
all.put("weather", weather);
// all.put("shzs", shzs);
//all.put("weatherinfo", weatherinfo);
return new Gson().toJson(all);
}
public String query(String lat, String lon) throws IOException {
String url = this.url.replace("${lat}", lat).replace("${lon}", lon);
return this.query(url);
}
}
最后附上与程序相关的天气图片,这可是我们美工姐姐一个一个切出来,并且把名称对应上的昂。
资源上传到csdn上了,有需要的可以下载去看看
http://download.csdn.net/download/qq_32376365/10207440
用法
lat:维度
lon:经度
这里可以接入定位SDK.
weatherUtils.query(lat, lon)
网友评论