關(guān)于java獲取新浪天氣示例
前言:
提供天氣api的廠商有很多,比如,騰訊、雅虎、中國天氣網(wǎng),在綜合比較各個(gè)功能后,決定使用新浪的天氣接口,主要是考慮到,新浪的接口可以直接通過城市名字查詢天氣,而像雅虎、中國天氣網(wǎng)需要使用自己的內(nèi)部城市編碼,維護(hù)起來比較麻煩,另外有的廠商會收費(fèi)。
實(shí)現(xiàn)思路:
- 通過新浪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需要經(jīng)過gbk編碼,比如StringencodedCityName = URLEncoder.encode(cityName, "GBK");
password是固定的,新浪規(guī)定的,不需要修改;
基本代碼:
通過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();
//服務(wù)器端使用utf-8編碼,所以這里用utf-8解析服務(wù)器端傳來的字節(jié)流
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("根據(jù)ip獲取城市名字失敗",e);
}
return "";
}這里使用了apache httpclient包,google的gson包,用于將json串轉(zhuǎn)為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("根據(jù)ip獲取城市名字失敗",e);
}
return null;
}這里用到了:jdk1.6自帶的jaxb(java api for xml binding)用于將xml轉(zhuǎn)java對象
輔助的實(shí)體類:
public class IpCityInfo implements Serializable{
/**
* serialVersionUID:TODO(用一句話描述這個(gè)變量表示什么)
*
* @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(用一句話描述這個(gè)變量表示什么)
*
* @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>北風(fēng)</direction1>
<direction2>北風(fēng)</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恤衫、敞領(lǐng)短袖棉衫</chy_shuoming>
<pollution_l>優(yōu)</pollution_l>
<zwx_l>強(qiáng)</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>非常有利于空氣污染物擴(kuò)散</pollution_s>
<zwx_s>紫外線強(qiáng)</zwx_s>
<ssd_s>戶外活動(dòng)不適宜在中午前后展開。</ssd_s>
<ktk_s>不需要開啟空調(diào)</ktk_s>
<xcz_s>洗車后未來1-2天內(nèi)有降水、大風(fēng)或沙塵天氣,不太適宜洗車</xcz_s>
<gm>1</gm>
<gm_l>低發(fā)期</gm_l>
<gm_s>環(huán)境溫度較高,要提防長時(shí)間在空調(diào)環(huán)境中引發(fā)的空調(diào)??;</gm_s>
<yd>5</yd>
<yd_l>不適宜</yd_l>
<yd_s>天氣炎熱,不適宜戶外運(yùn)動(dòng);</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的節(jié)點(diǎn)名字相同, @XmlElement是與xml文檔中的節(jié)點(diǎn)對應(yīng)的,@XmlElementxml只能添加在get/set方法上,添加在成員變量上會出現(xiàn)異常。 @xmlattribute是對應(yīng)xml文檔中的屬性,比如<node id="xx">是對應(yīng)id的。
到此這篇關(guān)于關(guān)于java獲取新浪天氣示例的文章就介紹到這了,更多相關(guān)java獲取新浪天氣內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot實(shí)現(xiàn)數(shù)據(jù)訪問計(jì)數(shù)器方案詳解
在Spring Boot項(xiàng)目中,有時(shí)需要數(shù)據(jù)訪問計(jì)數(shù)器,怎么實(shí)現(xiàn)數(shù)據(jù)訪問計(jì)數(shù)器呢?下面小編給大家?guī)砹薙pring Boot數(shù)據(jù)訪問計(jì)數(shù)器的實(shí)現(xiàn)方案,需要的朋友參考下吧2021-08-08
Java?+?Selenium?+?OpenCV解決自動(dòng)化測試中的滑塊驗(yàn)證問題
OpenCV是一個(gè)基于Apache2.0許可(開源)發(fā)行的跨平臺計(jì)算機(jī)視覺和機(jī)器學(xué)習(xí)軟件庫,可以運(yùn)行在Linux、Windows、Android和Mac?OS操作系統(tǒng)上,這篇文章主要介紹了Java?+?Selenium?+?OpenCV解決自動(dòng)化測試中的滑塊驗(yàn)證,需要的朋友可以參考下2022-07-07
詳談Java中instanceof和isInstance的區(qū)別
下面小編就為大家?guī)硪黄斦凧ava中instanceof和isInstance的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
SpringAop @Around執(zhí)行兩次的原因及解決
這篇文章主要介紹了SpringAop @Around執(zhí)行兩次的原因及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
詳解Spring boot/Spring 統(tǒng)一錯(cuò)誤處理方案的使用
這篇文章主要介紹了詳解Spring boot/Spring 統(tǒng)一錯(cuò)誤處理方案的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
java在cmd運(yùn)行"-d"和"-cp"參數(shù)解讀
這篇文章主要介紹了java在cmd運(yùn)行"-d"和"-cp"參數(shù)用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
springboot2如何集成ElasticSearch6.4.3
這篇文章主要介紹了springboot2如何集成ElasticSearch6.4.3問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

