您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“java怎么獲取新浪天氣”,內容詳細,步驟清晰,細節處理妥當,希望這篇“java怎么獲取新浪天氣”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
通過新浪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是固定的,新浪規定的,不需要修改;
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的。
讀到這里,這篇“java怎么獲取新浪天氣”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。