java怎么获取新浪天气

其他教程   发布日期:2025年04月21日   浏览次数:217

本文小编为大家详细介绍“java怎么获取新浪天气”,内容详细,步骤清晰,细节处理妥当,希望这篇“java怎么获取新浪天气”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

实现思路:

  1. 通过新浪ip的api,获取ip归属地城市;

  2. 通过新浪天气的api,传入城市名字,获取城市的天气;

  3. 通过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获取城市

    1. private static String getCityNameByIp(String ip) {
    2. try{
    3. String ipUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ip;
    4. HttpClient client = new DefaultHttpClient();
    5. HttpGet get = new HttpGet(ipUrl);
    6. HttpResponse response = client.execute(get);
    7. HttpEntity responseEntity = response.getEntity();
    8. //服务器端使用utf-8编码,所以这里用utf-8解析服务器端传来的字节流
    9. String resultJsonStr = EntityUtils.toString(responseEntity,"utf-8");
    10. IpCityInfo result = new Gson().fromJson(resultJsonStr, IpCityInfo.class);
    11. String cityName = result.getCity();
    12. if(cityName == null){
    13. return "";
    14. }
    15. }catch(Exception e){
    16. e.printStackTrace();
    17. logger.error("根据ip获取城市名字失败",e);
    18. }
    19. return "";
    20. }

    这里使用了apache httpclient包,google的gson包,用于将json串转为java对象

    通过城市名字获取天气

    1. private static WeatherInfo getWeatherInfoByCityName(String cityName) {
    2. String encodedCityName = null;
    3. try {
    4. encodedCityName = URLEncoder.encode(cityName, "GBK");
    5. } catch (UnsupportedEncodingException e1) {
    6. e1.printStackTrace();
    7. }
    8. String url = "http://php.weather.sina.com.cn/xml.php?city=" + encodedCityName
    9. + "&password=DJOYnieT8234jlsK&day=0";
    10. try{
    11. HttpClient client = new DefaultHttpClient();
    12. HttpGet get = new HttpGet(url);
    13. HttpResponse response = client.execute(get);
    14. HttpEntity responseEntity = response.getEntity();
    15. InputStream content = responseEntity.getContent();
    16. JAXBContext context = JAXBContext.newInstance(Profile.class);
    17. Unmarshaller unmarshaller = context.createUnmarshaller();
    18. Profile result = (Profile) unmarshaller.unmarshal(content);
    19. return result.getWeather();
    20. }catch(Exception e){
    21. e.printStackTrace();
    22. logger.error("根据ip获取城市名字失败",e);
    23. }
    24. return null;
    25. }

    这里用到了:jdk1.6自带的jaxb(java api for xml binding)用于将xml转java对象

    辅助的实体类:

    1. public class IpCityInfo implements Serializable{
    2. /**
    3. * serialVersionUID:TODO(用一句话描述这个变量表示什么)
    4. *
    5. * @since Ver 1.1
    6. */
    7. private static final long serialVersionUID = 1L;
    8. String ret;
    9. String start;
    10. String end;
    11. String country;
    12. String province;
    13. String city;
    14. String district;
    15. String isp;
    16. String type;
    17. String desc;
    18. public String getRet() {
    19. return ret;
    20. }
    21. public void setRet(String ret) {
    22. this.ret = ret;
    23. }
    24. public String getStart() {
    25. return start;
    26. }
    27. public void setStart(String start) {
    28. this.start = start;
    29. }
    30. public String getEnd() {
    31. return end;
    32. }
    33. public void setEnd(String end) {
    34. this.end = end;
    35. }
    36. public String getCountry() {
    37. return country;
    38. }
    39. public void setCountry(String country) {
    40. this.country = country;
    41. }
    42. public String getProvince() {
    43. return province;
    44. }
    45. public void setProvince(String province) {
    46. this.province = province;
    47. }
    48. public String getCity() {
    49. return city;
    50. }
    51. public void setCity(String city) {
    52. this.city = city;
    53. }
    54. public String getDistrict() {
    55. return district;
    56. }
    57. public void setDistrict(String district) {
    58. this.district = district;
    59. }
    60. public String getIsp() {
    61. return isp;
    62. }
    63. public void setIsp(String isp) {
    64. this.isp = isp;
    65. }
    66. public String getType() {
    67. return type;
    68. }
    69. public void setType(String type) {
    70. this.type = type;
    71. }
    72. public String getDesc() {
    73. return desc;
    74. }
    75. public void setDesc(String desc) {
    76. this.desc = desc;
    77. }
    78. @Override
    79. public String toString() {
    80. return "IpCityInfo [ret=" + ret + ", start=" + start + ", end=" + end
    81. + ", country=" + country + ", province=" + province + ", city="
    82. + city + ", district=" + district + ", isp=" + isp + ", type="
    83. + type + ", desc=" + desc + "]";
    84. }
    85. }
    1. @XmlRootElement(name="Profiles")
    2. public class Profile {
    3. private WeatherInfo weather;
    4. public WeatherInfo getWeather() {
    5. return weather;
    6. }
    7. @XmlElement(name="Weather")
    8. public void setWeather(WeatherInfo weather) {
    9. this.weather = weather;
    10. }
    11. }
    1. public class WeatherInfo implements Serializable{
    2. /**
    3. * serialVersionUID:TODO(用一句话描述这个变量表示什么)
    4. *
    5. * @since Ver 1.1
    6. */
    7. /*
    8. * <?xml version="1.0" encoding="UTF-8"?>
    9. <!-- published at 2015-07-01 10:26:29 -->
    10. <Profiles>
    11. <Weather>
    12. <city>青岛</city>
    13. <status1>晴</status1>
    14. <status2>晴</status2>
    15. <figure1>qing</figure1>
    16. <figure2>qing</figure2>
    17. <direction1>北风</direction1>
    18. <direction2>北风</direction2>
    19. <power1>3-4</power1>
    20. <power2>3-4</power2>
    21. <temperature1>31</temperature1>
    22. <temperature2>19</temperature2>
    23. <ssd>8</ssd>
    24. <tgd1>28</tgd1>
    25. <tgd2>28</tgd2>
    26. <zwx>4</zwx>
    27. <ktk>4</ktk>
    28. <pollution>1</pollution>
    29. <xcz>4</xcz>
    30. <zho></zho>
    31. <diy></diy>
    32. <fas></fas>
    33. <chy>1</chy>
    34. <zho_shuoming>暂无</zho_shuoming>
    35. <diy_shuoming>暂无</diy_shuoming>
    36. <fas_shuoming>暂无</fas_shuoming>
    37. <chy_shuoming>短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫</chy_shuoming>
    38. <pollution_l>优</pollution_l>
    39. <zwx_l>强</zwx_l>
    40. <ssd_l>较热</ssd_l>
    41. <fas_l>暂无</fas_l>
    42. <zho_l>暂无</zho_l>
    43. <chy_l>薄短袖类</chy_l>
    44. <ktk_l>不需要开启</ktk_l>
    45. <xcz_l>不太适宜</xcz_l>
    46. <diy_l>暂无</diy_l>
    47. <pollution_s>非常有利于空气污染物扩散</pollution_s>
    48. <zwx_s>紫外线强</zwx_s>
    49. <ssd_s>户外活动不适宜在中午前后展开。</ssd_s>
    50. <ktk_s>不需要开启空调</ktk_s>
    51. <xcz_s>洗车后未来1-2天内有降水、大风或沙尘天气,不太适宜洗车</xcz_s>
    52. <gm>1</gm>
    53. <gm_l>低发期</gm_l>
    54. <gm_s>环境温度较高,要提防长时间在空调环境中引发的空调病;</gm_s>
    55. <yd>5</yd>
    56. <yd_l>不适宜</yd_l>
    57. <yd_s>天气炎热,不适宜户外运动;</yd_s>
    58. <savedate_weather>2015-07-01</savedate_weather>
    59. <savedate_life>2015-07-01</savedate_life>
    60. <savedate_zhishu>2015-07-01</savedate_zhishu>
    61. <udatetime>2015-07-01 07:58:00</udatetime>
    62. </Weather>
    63. </Profiles>
    64. */
    65. private static final long serialVersionUID = 1L;
    66. private String city;
    67. private String status1;
    68. private String status2;
    69. private String figure1;
    70. private String figure2;
    71. private String direction1;
    72. private String direction2;
    73. private String power1;
    74. private String power2;
    75. private String temperature1;
    76. private String temperature2;
    77. private String ssd;
    78. private String tgd1;
    79. private String tgd2;
    80. private String zwx;
    81. private String ktk;
    82. private String pollution;
    83. private String xcz;
    84. private String zho;
    85. private String diy;
    86. private String fas;
    87. private String chy;
    88. private String zho_shuoming;
    89. private String diy_shuoming;
    90. private String fas_shuoming;
    91. private String chy_shuoming;
    92. private String pollution_l;
    93. private String zwx_l;
    94. private String ssd_l;
    95. private String fas_l;
    96. private String zho_l;
    97. private String chy_l;
    98. private String ktk_l;
    99. private String xcz_l;
    100. private String diy_l;
    101. private String pollution_s;
    102. private String zwx_s;
    103. private String ssd_s;
    104. private String ktk_s;
    105. private String xcz_s;
    106. private String gm;
    107. private String gm_l;
    108. private String gm_s;
    109. private String yd;
    110. private String yd_l;
    111. private String yd_s;
    112. private String savedate_weather;
    113. private String savedate_life;
    114. private String savedate_zhishu;
    115. private String udatetime;
    116. public String getCity() {
    117. return city;
    118. }
    119. public void setCity(String city) {
    120. this.city = city;
    121. }
    122. public String getStatus1() {
    123. return status1;
    124. }
    125. public void setStatus1(String status1) {
    126. this.status1 = status1;
    127. }
    128. public String getStatus2() {
    129. return status2;
    130. }
    131. public void setStatus2(String status2) {
    132. this.status2 = status2;
    133. }
    134. public String getFigure1() {
    135. return figure1;
    136. }
    137. public void setFigure1(String figure1) {
    138. this.figure1 = figure1;
    139. }
    140. public String getFigure2() {
    141. return figure2;
    142. }
    143. public void setFigure2(String figure2) {
    144. this.figure2 = figure2;
    145. }
    146. public String getDirection1() {
    147. return direction1;
    148. }
    149. public void setDirection1(String direction1) {
    150. this.direction1 = direction1;
    151. }
    152. public String getDirection2() {
    153. return direction2;
    154. }
    155. public void setDirection2(String direction2) {
    156. this.direction2 = direction2;
    157. }
    158. public String getPower1() {
    159. return power1;
    160. }
    161. public void setPower1(String power1) {
    162. this.power1 = power1;
    163. }
    164. public String getPower2() {
    165. return power2;
    166. }
    167. public void setPower2(String power2) {
    168. this.power2 = power2;
    169. }
    170. public String getTemperature1() {
    171. return temperature1;
    172. }
    173. public void setTemperature1(String temperature1) {
    174. this.temperature1 = temperature1;
    175. }
    176. public String getTemperature2() {
    177. return temperature2;
    178. }
    179. public void setTemperature2(String temperature2) {
    180. this.temperature2 = temperature2;
    181. }
    182. public String getSsd() {
    183. return ssd;
    184. }
    185. public void setSsd(String ssd) {
    186. this.ssd = ssd;
    187. }
    188. public String getTgd1() {
    189. return tgd1;
    190. }
    191. public void setTgd1(String tgd1) {
    192. this.tgd1 = tgd1;
    193. }
    194. public String getTgd2() {
    195. return tgd2;
    196. }
    197. public void setTgd2(String tgd2) {
    198. this.tgd2 = tgd2;
    199. }
    200. public String getZwx() {
    201. return zwx;
    202. }
    203. public void setZwx(String zwx) {
    204. this.zwx = zwx;
    205. }
    206. public String getKtk() {
    207. return ktk;
    208. }
    209. public void setKtk(String ktk) {
    210. this.ktk = ktk;
    211. }
    212. public String getPollution() {
    213. return pollution;
    214. }
    215. public void setPollution(String pollution) {
    216. this.pollution = pollution;
    217. }
    218. public String getXcz() {
    219. return xcz;
    220. }
    221. public void setXcz(String xcz) {
    222. this.xcz = xcz;
    223. }
    224. public String getZho() {
    225. return zho;
    226. }
    227. public void setZho(String zho) {
    228. this.zho = zho;
    229. }
    230. public String getDiy() {
    231. return diy;
    232. }
    233. public void setDiy(String diy) {
    234. this.diy = diy;
    235. }
    236. public String getFas() {
    237. return fas;
    238. }
    239. public void setFas(String fas) {
    240. this.fas = fas;
    241. }
    242. public String getChy() {
    243. return chy;
    244. }
    245. public void setChy(String chy) {
    246. this.chy = chy;
    247. }
    248. public String getZho_shuoming() {
    249. return zho_shuoming;
    250. }
    251. public void setZho_shuoming(String zho_shuoming) {
    252. this.zho_shuoming = zho_shuoming;
    253. }
    254. public String getDiy_shuoming() {
    255. return diy_shuoming;
    256. }
    257. public void setDiy_shuoming(String diy_shuoming) {
    258. this.diy_shuoming = diy_shuoming;
    259. }
    260. public String getFas_shuoming() {
    261. return fas_shuoming;
    262. }
    263. public void setFas_shuoming(String fas_shuoming) {
    264. this.fas_shuoming = fas_shuoming;
    265. }
    266. public String getChy_shuoming() {
    267. return chy_shuoming;
    268. }
    269. public void setChy_shuoming(String chy_shuoming) {
    270. this.chy_shuoming = chy_shuoming;
    271. }
    272. public String getPollution_l() {
    273. return pollution_l;
    274. }
    275. public void setPollution_l(String pollution_l) {
    276. this.pollution_l = pollution_l;
    277. }
    278. public String getZwx_l() {
    279. return zwx_l;
    280. }
    281. public void setZwx_l(String zwx_l) {
    282. this.zwx_l = zwx_l;
    283. }
    284. public String getSsd_l() {
    285. return ssd_l;
    286. }
    287. public void setSsd_l(String ssd_l) {
    288. this.ssd_l = ssd_l;
    289. }
    290. public String getFas_l() {
    291. return fas_l;
    292. }
    293. public void setFas_l(String fas_l) {
    294. this.fas_l = fas_l;
    295. }
    296. public String getZho_l() {
    297. return zho_l;
    298. }
    299. public void setZho_l(String zho_l) {
    300. this.zho_l = zho_l;
    301. }
    302. public String getChy_l() {
    303. return chy_l;
    304. }
    305. public void setChy_l(String chy_l) {
    306. this.chy_l = chy_l;
    307. }
    308. public String getKtk_l() {
    309. return ktk_l;
    310. }
    311. public void setKtk_l(String ktk_l) {
    312. this.ktk_l = ktk_l;
    313. }
    314. public String getXcz_l() {
    315. return xcz_l;
    316. }
    317. public void setXcz_l(String xcz_l) {
    318. this.xcz_l = xcz_l;
    319. }
    320. public String getDiy_l() {
    321. return diy_l;
    322. }
    323. public void setDiy_l(String diy_l) {
    324. this.diy_l = diy_l;
    325. }
    326. public String getPollution_s() {
    327. return pollution_s;
    328. }
    329. public void setPollution_s(String pollution_s) {
    330. this.pollution_s = pollution_s;
    331. }
    332. public String getZwx_s() {
    333. return zwx_s;
    334. }
    335. public void setZwx_s(String zwx_s) {
    336. this.zwx_s = zwx_s;
    337. }
    338. public String getSsd_s() {
    339. return ssd_s;
    340. }
    341. public void setSsd_s(String ssd_s) {
    342. this.ssd_s = ssd_s;
    343. }
    344. public String getKtk_s() {
    345. return ktk_s;
    346. }
    347. public void setKtk_s(String ktk_s) {
    348. this.ktk_s = ktk_s;
    349. }
    350. public String getXcz_s() {
    351. return xcz_s;
    352. }
    353. public void setXcz_s(String xcz_s) {
    354. this.xcz_s = xcz_s;
    355. }
    356. public String getGm() {
    357. return gm;
    358. }
    359. public void setGm(String gm) {
    360. this.gm = gm;
    361. }
    362. public String getGm_l() {
    363. return gm_l;
    364. }
    365. public void setGm_l(String gm_l) {
    366. this.gm_l = gm_l;
    367. }
    368. public String getGm_s() {
    369. return gm_s;
    370. }
    371. public void setGm_s(String gm_s) {
    372. this.gm_s = gm_s;
    373. }
    374. public String getYd() {
    375. return yd;
    376. }
    377. public void setYd(String yd) {
    378. this.yd = yd;
    379. }
    380. public String getYd_l() {
    381. return yd_l;
    382. }
    383. public void setYd_l(String yd_l) {
    384. this.yd_l = yd_l;
    385. }
    386. public String getYd_s() {
    387. return yd_s;
    388. }
    389. public void setYd_s(String yd_s) {
    390. this.yd_s = yd_s;
    391. }
    392. public String getSavedate_weather() {
    393. return savedate_weather;
    394. }
    395. public void setSavedate_weather(String savedate_weather) {
    396. this.savedate_weather = savedate_weather;
    397. }
    398. public String getSavedate_life() {
    399. return savedate_life;
    400. }
    401. public void setSavedate_life(String savedate_life) {
    402. this.savedate_life = savedate_life;
    403. }
    404. public String getSavedate_zhishu() {
    405. return savedate_zhishu;
    406. }
    407. public void setSavedate_zhishu(String savedate_zhishu) {
    408. this.savedate_zhishu = savedate_zhishu;
    409. }
    410. public String getUdatetime() {
    411. return udatetime;
    412. }
    413. public void setUdatetime(String udatetime) {
    414. this.udatetime = udatetime;
    415. }
    416. @Override
    417. public String toString() {
    418. return "WeatherInfo [city=" + city + ", status1=" + status1
    419. + ", status2=" + status2 + ", figure1=" + figure1
    420. + ", figure2=" + figure2 + ", direction1=" + direction1
    421. + ", direction2=" + direction2 + ", power1=" + power1
    422. + ", power2=" + power2 + ", temperature1=" + temperature1
    423. + ", temperature2=" + temperature2 + ", ssd=" + ssd + ", tgd1="
    424. + tgd1 + ", tgd2=" + tgd2 + ", zwx=" + zwx + ", ktk=" + ktk
    425. + ", pollution=" + pollution + ", xcz=" + xcz + ", zho=" + zho
    426. + ", diy=" + diy + ", fas=" + fas + ", chy=" + chy
    427. + ", zho_shuoming=" + zho_shuoming + ", diy_shuoming="
    428. + diy_shuoming + ", fas_shuoming=" + fas_shuoming
    429. + ", chy_shuoming=" + chy_shuoming + ", pollution_l="
    430. + pollution_l + ", zwx_l=" + zwx_l + ", ssd_l=" + ssd_l
    431. + ", fas_l=" + fas_l + ", zho_l=" + zho_l + ", chy_l=" + chy_l
    432. + ", ktk_l=" + ktk_l + ", xcz_l=" + xcz_l + ", diy_l=" + diy_l
    433. + ", pollution_s=" + pollution_s + ", zwx_s=" + zwx_s
    434. + ", ssd_s=" + ssd_s + ", ktk_s=" + ktk_s + ", xcz_s=" + xcz_s
    435. + ", gm=" + gm + ", gm_l=" + gm_l + ", gm_s=" + gm_s + ", yd="
    436. + yd + ", yd_l=" + yd_l + ", yd_s=" + yd_s
    437. + ", savedate_weather=" + savedate_weather + ", savedate_life="
    438. + savedate_life + ", savedate_zhishu=" + savedate_zhishu
    439. + ", udatetime=" + udatetime + "]";
    440. }
    441. }

    过程中遇到的问题:

    由于是第一次使用jaxb,对要用到的注解不熟悉,这里再说明一下, 根元素需要添加注解@XmlRootElement(name="xx") , name值与xml的节点名字相同, @XmlElement是与xml文档中的节点对应的,@XmlElementxml只能添加在get/set方法上,添加在成员变量上会出现异常。 @xmlattribute是对应xml文档中的属性,比如<node id="xx">是对应id的。

    以上就是java怎么获取新浪天气的详细内容,更多关于java怎么获取新浪天气的资料请关注九品源码其它相关文章!