前言:
提供天气api的厂商有很多,比如,腾讯、雅虎、中国天气网,在综合比较各个功能后,决定使用新浪的天气接口,主要是考虑到,新浪的接口可以直接通过城市名字查询天气,而像雅虎、中国天气网需要使用自己的内部城市编码,维护起来比较麻烦,另外有的厂商会收费。
实现思路:
- 通过新浪ip的api,获取ip归属地城市;
- 通过新浪天气的api,传入城市名字,获取城市的天气;
- 通过ip获取城市的url地址:
- //int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ip
- 通过城市获取天气的url地址:
- "//php.weather.sina.com.cn/xml.php?city=" + encodedCityName+ "&password=DJOYnieT8234jlsK&day=0";
注意,city需要经过gbk编码,比如StringencodedCityName = URLEncoder.encode(cityName, "GBK");
password是固定的,新浪规定的,不需要修改;
基本代码:
通过ip获取城市
private static String getCityNameByIp(String ip) { try{ String ipUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ip; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(ipUrl); HttpResponse response = client.execute(get); HttpEntity responseEntity = response.getEntity(); //服务器端使用utf-8编码,所以这里用utf-8解析服务器端传来的字节流 String resultJsonStr = EntityUtils.toString(responseEntity,"utf-8"); IpCityInfo result = new Gson().fromJson(resultJsonStr, IpCityInfo.class); String cityName = result.getCity(); if(cityName == null){ return ""; } }catch(Exception e){ e.printStackTrace(); logger.error("根据ip获取城市名字失败",e); } return ""; }
这里使用了apache httpclient包,google的gson包,用于将json串转为java对象
通过城市名字获取天气
private static WeatherInfo getWeatherInfoByCityName(String cityName) { String encodedCityName = null; try { encodedCityName = URLEncoder.encode(cityName, "GBK"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } String url = "http://php.weather.sina.com.cn/xml.php?city=" + encodedCityName + "&password=DJOYnieT8234jlsK&day=0"; try{ HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); HttpEntity responseEntity = response.getEntity(); InputStream content = responseEntity.getContent(); JAXBContext context = JAXBContext.newInstance(Profile.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Profile result = (Profile) unmarshaller.unmarshal(content); return result.getWeather(); }catch(Exception e){ e.printStackTrace(); logger.error("根据ip获取城市名字失败",e); } return null; }
这里用到了:jdk1.6自带的jaxb(java api for xml binding)用于将xml转java对象
辅助的实体类:
public class IpCityInfo implements Serializable{ /** * serialVersionUID:TODO(用一句话描述这个变量表示什么) * * @since Ver 1.1 */ private static final long serialVersionUID = 1L; String ret; String start; String end; String country; String province; String city; String district; String isp; String type; String desc; public String getRet() { return ret; } public void setRet(String ret) { this.ret = ret; } public String getStart() { return start; } public void setStart(String start) { this.start = start; } public String getEnd() { return end; } public void setEnd(String end) { this.end = end; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getIsp() { return isp; } public void setIsp(String isp) { this.isp = isp; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "IpCityInfo [ret=" + ret + ", start=" + start + ", end=" + end + ", country=" + country + ", province=" + province + ", city=" + city + ", district=" + district + ", isp=" + isp + ", type=" + type + ", desc=" + desc + "]"; } }
@XmlRootElement(name="Profiles") public class Profile { private WeatherInfo weather; public WeatherInfo getWeather() { return weather; } @XmlElement(name="Weather") public void setWeather(WeatherInfo weather) { this.weather = weather; } }
public class WeatherInfo implements Serializable{ /** * serialVersionUID:TODO(用一句话描述这个变量表示什么) * * @since Ver 1.1 */ /* * <?xml version="1.0" encoding="UTF-8"?> <!-- published at 2015-07-01 10:26:29 --> <Profiles> <Weather> <city>青岛</city> <status1>晴</status1> <status2>晴</status2> <figure1>qing</figure1> <figure2>qing</figure2> <direction1>北风</direction1> <direction2>北风</direction2> <power1>3-4</power1> <power2>3-4</power2> <temperature1>31</temperature1> <temperature2>19</temperature2> <ssd>8</ssd> <tgd1>28</tgd1> <tgd2>28</tgd2> <zwx>4</zwx> <ktk>4</ktk> <pollution>1</pollution> <xcz>4</xcz> <zho></zho> <diy></diy> <fas></fas> <chy>1</chy> <zho_shuoming>暂无</zho_shuoming> <diy_shuoming>暂无</diy_shuoming> <fas_shuoming>暂无</fas_shuoming> <chy_shuoming>短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫</chy_shuoming> <pollution_l>优</pollution_l> <zwx_l>强</zwx_l> <ssd_l>较热</ssd_l> <fas_l>暂无</fas_l> <zho_l>暂无</zho_l> <chy_l>薄短袖类</chy_l> <ktk_l>不需要开启</ktk_l> <xcz_l>不太适宜</xcz_l> <diy_l>暂无</diy_l> <pollution_s>非常有利于空气污染物扩散</pollution_s> <zwx_s>紫外线强</zwx_s> <ssd_s>户外活动不适宜在中午前后展开。</ssd_s> <ktk_s>不需要开启空调</ktk_s> <xcz_s>洗车后未来1-2天内有降水、大风或沙尘天气,不太适宜洗车</xcz_s> <gm>1</gm> <gm_l>低发期</gm_l> <gm_s>环境温度较高,要提防长时间在空调环境中引发的空调病;</gm_s> <yd>5</yd> <yd_l>不适宜</yd_l> <yd_s>天气炎热,不适宜户外运动;</yd_s> <savedate_weather>2015-07-01</savedate_weather> <savedate_life>2015-07-01</savedate_life> <savedate_zhishu>2015-07-01</savedate_zhishu> <udatetime>2015-07-01 07:58:00</udatetime> </Weather> </Profiles> */ private static final long serialVersionUID = 1L; private String city; private String status1; private String status2; private String figure1; private String figure2; private String direction1; private String direction2; private String power1; private String power2; private String temperature1; private String temperature2; private String ssd; private String tgd1; private String tgd2; private String zwx; private String ktk; private String pollution; private String xcz; private String zho; private String diy; private String fas; private String chy; private String zho_shuoming; private String diy_shuoming; private String fas_shuoming; private String chy_shuoming; private String pollution_l; private String zwx_l; private String ssd_l; private String fas_l; private String zho_l; private String chy_l; private String ktk_l; private String xcz_l; private String diy_l; private String pollution_s; private String zwx_s; private String ssd_s; private String ktk_s; private String xcz_s; private String gm; private String gm_l; private String gm_s; private String yd; private String yd_l; private String yd_s; private String savedate_weather; private String savedate_life; private String savedate_zhishu; private String udatetime; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStatus1() { return status1; } public void setStatus1(String status1) { this.status1 = status1; } public String getStatus2() { return status2; } public void setStatus2(String status2) { this.status2 = status2; } public String getFigure1() { return figure1; } public void setFigure1(String figure1) { this.figure1 = figure1; } public String getFigure2() { return figure2; } public void setFigure2(String figure2) { this.figure2 = figure2; } public String getDirection1() { return direction1; } public void setDirection1(String direction1) { this.direction1 = direction1; } public String getDirection2() { return direction2; } public void setDirection2(String direction2) { this.direction2 = direction2; } public String getPower1() { return power1; } public void setPower1(String power1) { this.power1 = power1; } public String getPower2() { return power2; } public void setPower2(String power2) { this.power2 = power2; } public String getTemperature1() { return temperature1; } public void setTemperature1(String temperature1) { this.temperature1 = temperature1; } public String getTemperature2() { return temperature2; } public void setTemperature2(String temperature2) { this.temperature2 = temperature2; } public String getSsd() { return ssd; } public void setSsd(String ssd) { this.ssd = ssd; } public String getTgd1() { return tgd1; } public void setTgd1(String tgd1) { this.tgd1 = tgd1; } public String getTgd2() { return tgd2; } public void setTgd2(String tgd2) { this.tgd2 = tgd2; } public String getZwx() { return zwx; } public void setZwx(String zwx) { this.zwx = zwx; } public String getKtk() { return ktk; } public void setKtk(String ktk) { this.ktk = ktk; } public String getPollution() { return pollution; } public void setPollution(String pollution) { this.pollution = pollution; } public String getXcz() { return xcz; } public void setXcz(String xcz) { this.xcz = xcz; } public String getZho() { return zho; } public void setZho(String zho) { this.zho = zho; } public String getDiy() { return diy; } public void setDiy(String diy) { this.diy = diy; } public String getFas() { return fas; } public void setFas(String fas) { this.fas = fas; } public String getChy() { return chy; } public void setChy(String chy) { this.chy = chy; } public String getZho_shuoming() { return zho_shuoming; } public void setZho_shuoming(String zho_shuoming) { this.zho_shuoming = zho_shuoming; } public String getDiy_shuoming() { return diy_shuoming; } public void setDiy_shuoming(String diy_shuoming) { this.diy_shuoming = diy_shuoming; } public String getFas_shuoming() { return fas_shuoming; } public void setFas_shuoming(String fas_shuoming) { this.fas_shuoming = fas_shuoming; } public String getChy_shuoming() { return chy_shuoming; } public void setChy_shuoming(String chy_shuoming) { this.chy_shuoming = chy_shuoming; } public String getPollution_l() { return pollution_l; } public void setPollution_l(String pollution_l) { this.pollution_l = pollution_l; } public String getZwx_l() { return zwx_l; } public void setZwx_l(String zwx_l) { this.zwx_l = zwx_l; } public String getSsd_l() { return ssd_l; } public void setSsd_l(String ssd_l) { this.ssd_l = ssd_l; } public String getFas_l() { return fas_l; } public void setFas_l(String fas_l) { this.fas_l = fas_l; } public String getZho_l() { return zho_l; } public void setZho_l(String zho_l) { this.zho_l = zho_l; } public String getChy_l() { return chy_l; } public void setChy_l(String chy_l) { this.chy_l = chy_l; } public String getKtk_l() { return ktk_l; } public void setKtk_l(String ktk_l) { this.ktk_l = ktk_l; } public String getXcz_l() { return xcz_l; } public void setXcz_l(String xcz_l) { this.xcz_l = xcz_l; } public String getDiy_l() { return diy_l; } public void setDiy_l(String diy_l) { this.diy_l = diy_l; } public String getPollution_s() { return pollution_s; } public void setPollution_s(String pollution_s) { this.pollution_s = pollution_s; } public String getZwx_s() { return zwx_s; } public void setZwx_s(String zwx_s) { this.zwx_s = zwx_s; } public String getSsd_s() { return ssd_s; } public void setSsd_s(String ssd_s) { this.ssd_s = ssd_s; } public String getKtk_s() { return ktk_s; } public void setKtk_s(String ktk_s) { this.ktk_s = ktk_s; } public String getXcz_s() { return xcz_s; } public void setXcz_s(String xcz_s) { this.xcz_s = xcz_s; } public String getGm() { return gm; } public void setGm(String gm) { this.gm = gm; } public String getGm_l() { return gm_l; } public void setGm_l(String gm_l) { this.gm_l = gm_l; } public String getGm_s() { return gm_s; } public void setGm_s(String gm_s) { this.gm_s = gm_s; } public String getYd() { return yd; } public void setYd(String yd) { this.yd = yd; } public String getYd_l() { return yd_l; } public void setYd_l(String yd_l) { this.yd_l = yd_l; } public String getYd_s() { return yd_s; } public void setYd_s(String yd_s) { this.yd_s = yd_s; } public String getSavedate_weather() { return savedate_weather; } public void setSavedate_weather(String savedate_weather) { this.savedate_weather = savedate_weather; } public String getSavedate_life() { return savedate_life; } public void setSavedate_life(String savedate_life) { this.savedate_life = savedate_life; } public String getSavedate_zhishu() { return savedate_zhishu; } public void setSavedate_zhishu(String savedate_zhishu) { this.savedate_zhishu = savedate_zhishu; } public String getUdatetime() { return udatetime; } public void setUdatetime(String udatetime) { this.udatetime = udatetime; } @Override public String toString() { return "WeatherInfo [city=" + city + ", status1=" + status1 + ", status2=" + status2 + ", figure1=" + figure1 + ", figure2=" + figure2 + ", direction1=" + direction1 + ", direction2=" + direction2 + ", power1=" + power1 + ", power2=" + power2 + ", temperature1=" + temperature1 + ", temperature2=" + temperature2 + ", ssd=" + ssd + ", tgd1=" + tgd1 + ", tgd2=" + tgd2 + ", zwx=" + zwx + ", ktk=" + ktk + ", pollution=" + pollution + ", xcz=" + xcz + ", zho=" + zho + ", diy=" + diy + ", fas=" + fas + ", chy=" + chy + ", zho_shuoming=" + zho_shuoming + ", diy_shuoming=" + diy_shuoming + ", fas_shuoming=" + fas_shuoming + ", chy_shuoming=" + chy_shuoming + ", pollution_l=" + pollution_l + ", zwx_l=" + zwx_l + ", ssd_l=" + ssd_l + ", fas_l=" + fas_l + ", zho_l=" + zho_l + ", chy_l=" + chy_l + ", ktk_l=" + ktk_l + ", xcz_l=" + xcz_l + ", diy_l=" + diy_l + ", pollution_s=" + pollution_s + ", zwx_s=" + zwx_s + ", ssd_s=" + ssd_s + ", ktk_s=" + ktk_s + ", xcz_s=" + xcz_s + ", gm=" + gm + ", gm_l=" + gm_l + ", gm_s=" + gm_s + ", yd=" + yd + ", yd_l=" + yd_l + ", yd_s=" + yd_s + ", savedate_weather=" + savedate_weather + ", savedate_life=" + savedate_life + ", savedate_zhishu=" + savedate_zhishu + ", udatetime=" + udatetime + "]"; } }
过程中遇到的问题:
由于是第一次使用jaxb,对要用到的注解不熟悉,这里再说明一下, 根元素需要添加注解@XmlRootElement(name="xx") , name值与xml的节点名字相同, @XmlElement是与xml文档中的节点对应的,@XmlElementxml只能添加在get/set方法上,添加在成员变量上会出现异常。 @xmlattribute是对应xml文档中的属性,比如<node id="xx">是对应id的。
原文地址:https://wangjun5159.blog.csdn.net/article/details/46710509