A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 何家旺 黑马粉丝团   /  2013-8-20 22:40  /  14080 人查看  /  157 人回复  /   9 人收藏 转载请遵从CC协议 禁止商业使用本文

如题,调用API实现天气预报功能!
  1. //通过中国天气api调用
  2.         private String getWeatherInfo2(){
  3.                 StringBuilder info = new StringBuilder();
  4.                 try {
  5.                         DefaultHttpClient httpclient = new DefaultHttpClient();
  6.                         HttpGet httget = new HttpGet("http://m.weather.com.cn/data/101050101.html");
  7.                         ResponseHandler<String> responseHandler = new BasicResponseHandler();
  8.                         String responseBody = httpclient.execute(httget, responseHandler);
  9.                         System.out.println(responseBody);
  10.                         JsonParser jp = new JsonParser();
  11.                         JsonElement jse = jp.parse(responseBody);
  12.                         JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
  13. //                        String updTime = jso.get("fchh").getAsString();
  14. //                        if(updTime != null){
  15. //                                //温度
  16. //                                String j = jso.get("temp1").getAsString();//今天
  17. //                                String m = jso.get("temp2").getAsString();//明天
  18. //                                //天气情况
  19. //                                String j_weather = jso.get("weather1").getAsString();//今天
  20. //                                String m_weather = jso.get("weather2").getAsString();//明天
  21. //                                //风向风力
  22. //                                String j_wind = jso.get("wind1").getAsString();//今天
  23. //                                String m_wind = jso.get("wind2").getAsString();//明天
  24. //                                info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
  25. //                                info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
  26. //                        }
  27.                         String updTime = jso.get("fchh").getAsString();
  28.                         if(updTime != null){
  29.                                 if(!updTime.trim().equals("18")){
  30.                                         //温度
  31.                                         String j = jso.get("temp1").getAsString();//今天
  32.                                         String m = jso.get("temp2").getAsString();//明天
  33.                                         //天气情况
  34.                                         String j_weather = jso.get("weather1").getAsString();//今天
  35.                                         String m_weather = jso.get("weather2").getAsString();//明天
  36.                                         //风向风力
  37.                                         String j_wind = jso.get("wind1").getAsString();//今天
  38.                                         String m_wind = jso.get("wind2").getAsString();//明天
  39.                                         info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
  40.                                         info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
  41.                                 }else{
  42.                                         //18
  43.                                         //温度
  44.                                         String temp1 = jso.get("temp1").getAsString();//今天
  45.                                         String temp2 = jso.get("temp2").getAsString();//今天
  46.                                         String temp3 = jso.get("temp3").getAsString();//今天
  47.                                         String j = temp1.split("~")[1] + "~" +  temp2.split("~")[0];
  48.                                         String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
  49.                                         //天气情况
  50.                                         String weather1 = jso.get("weather1").getAsString();
  51.                                         String weather2 = jso.get("weather2").getAsString();
  52.                                         String weather3 = jso.get("weather3").getAsString();
  53.                                         String j_weather = "";
  54.                                         String j_weather_part1 = "";
  55.                                         String j_weather_part2 = "";
  56.                                         //判断是否有转
  57.                                         if(weather1.indexOf("转") > 0){
  58.                                                 //有
  59.                                                 j_weather_part1 = weather1.split("转")[1];
  60.                                         }else{
  61.                                                 j_weather_part1 = weather1;
  62.                                         }
  63.                                         if(weather2.indexOf("转") > 0){
  64.                                                 //有
  65.                                                 j_weather_part2 = weather2.split("转")[0];
  66.                                         }else{
  67.                                                 j_weather_part2 = weather2;
  68.                                         }
  69.                                         if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
  70.                                                 j_weather = j_weather_part1;//今天
  71.                                         }else{
  72.                                                 j_weather = j_weather_part1 + "转" + j_weather_part2;//今天
  73.                                         }
  74.                                         String m_weather = "";
  75.                                         String m_weather_part1 = "";
  76.                                         String m_weather_part2 = "";
  77.                                         //判断是否有转
  78.                                         if(weather2.indexOf("转") > 0){
  79.                                                 //有
  80.                                                 m_weather_part1 = weather2.split("转")[1];
  81.                                         }else{
  82.                                                 m_weather_part1 = weather2;
  83.                                         }
  84.                                         if(weather3.indexOf("转") > 0){
  85.                                                 //有
  86.                                                 m_weather_part2 = weather3.split("转")[0];
  87.                                         }else{
  88.                                                 m_weather_part2 = weather3;
  89.                                         }
  90.                                         if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
  91.                                                 m_weather = m_weather_part1;//今天
  92.                                         }else{
  93.                                                 m_weather = m_weather_part1 + "转" + m_weather_part2;//明天
  94.                                         }
复制代码
游客,如果您要查看本帖隐藏内容请回复

157 个回复

倒序浏览
好东西,学习了~~
回复 使用道具 举报
好东西当然要学习了。。
回复 使用道具 举报
好东西呀
回复 使用道具 举报
嗯...可以看看
回复 使用道具 举报
越来越佩服你了
回复 使用道具 举报
好东东唉唉唉
回复 使用道具 举报
HG_TYF 初级黑马 2013-8-27 22:39:56
8#
学习学习。
回复 使用道具 举报
看看好东西谢谢
回复 使用道具 举报
。。。。。。。。。。。。。。。。
回复 使用道具 举报
LOOK,顺带试试
回复 使用道具 举报
好东西,顶一个!
回复 使用道具 举报
还有更多内容啊
回复 使用道具 举报
试一下,看看什么情况
回复 使用道具 举报

好东西,学习了~~
回复 使用道具 举报
dddddddddddddddddddddddddddd
回复 使用道具 举报
不错过来看看
回复 使用道具 举报
风乐 中级黑马 2013-12-20 16:01:37
18#
调用中国天气网api获得天气预报信息
回复 使用道具 举报
chensc 金牌黑马 2013-12-21 15:09:39
19#
学习学习!
回复 使用道具 举报
chensc 金牌黑马 2013-12-26 08:20:04
20#
学习学习!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马