//通过中国天气api调用 |
002 | private String getWeatherInfo2(){ |
003 | StringBuilder info = new StringBuilder(); |
004 | try { |
005 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
006 | HttpGet httget = new HttpGet( "http://m.weather.com.cn/data/101050101.html" ); |
007 | ResponseHandler<String> responseHandler = new BasicResponseHandler(); |
008 | String responseBody = httpclient.execute(httget, responseHandler); |
009 | System.out.println(responseBody); |
010 | JsonParser jp = new JsonParser(); |
011 | JsonElement jse = jp.parse(responseBody); |
012 | JsonObject jso =jse.getAsJsonObject().get( "weatherinfo" ).getAsJsonObject(); |
013 | // String updTime = jso.get("fchh").getAsString(); |
014 | // if(updTime != null){ |
015 | // //温度 |
016 | // String j = jso.get("temp1").getAsString();//今天 |
017 | // String m = jso.get("temp2").getAsString();//明天 |
018 | // //天气情况 |
019 | // String j_weather = jso.get("weather1").getAsString();//今天 |
020 | // String m_weather = jso.get("weather2").getAsString();//明天 |
021 | // //风向风力 |
022 | // String j_wind = jso.get("wind1").getAsString();//今天 |
023 | // String m_wind = jso.get("wind2").getAsString();//明天 |
024 | // info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n"); |
025 | // info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n"); |
026 | // } |
027 | String updTime = jso.get( "fchh" ).getAsString(); |
028 | if (updTime != null ){ |
029 | if (!updTime.trim().equals( "18" )){ |
030 | //温度 |
031 | String j = jso.get( "temp1" ).getAsString(); //今天 |
032 | String m = jso.get( "temp2" ).getAsString(); //明天 |
033 | //天气情况 |
034 | String j_weather = jso.get( "weather1" ).getAsString(); //今天 |
035 | String m_weather = jso.get( "weather2" ).getAsString(); //明天 |
036 | //风向风力 |
037 | String j_wind = jso.get( "wind1" ).getAsString(); //今天 |
038 | String m_wind = jso.get( "wind2" ).getAsString(); //明天 |
039 | info.append( "今天:" ).append(j).append( " " ).append(j_weather).append( " " ).append(j_wind).append( "\n" ); |
040 | info.append( "明天:" ).append(m).append( " " ).append(m_weather).append( " " ).append(m_wind).append( "\n" ); |
041 | } else { |
042 | //18 |
043 | //温度 |
044 | String temp1 = jso.get( "temp1" ).getAsString(); //今天 |
045 | String temp2 = jso.get( "temp2" ).getAsString(); //今天 |
046 | String temp3 = jso.get( "temp3" ).getAsString(); //今天 |
047 | String j = temp1.split( "~" )[ 1 ] + "~" + temp2.split( "~" )[ 0 ]; |
048 | String m = temp2.split( "~" )[ 1 ] + "~" + temp3.split( "~" )[ 0 ]; //明天 |
049 | //天气情况 |
050 | String weather1 = jso.get( "weather1" ).getAsString(); |
051 | String weather2 = jso.get( "weather2" ).getAsString(); |
052 | String weather3 = jso.get( "weather3" ).getAsString(); |
053 | String j_weather = "" ; |
054 | String j_weather_part1 = "" ; |
055 | String j_weather_part2 = "" ; |
056 | //判断是否有转 |
057 | if (weather1.indexOf( "转" ) > 0 ){ |
058 | //有 |
059 | j_weather_part1 = weather1.split( "转" )[ 1 ]; |
060 | } else { |
061 | j_weather_part1 = weather1; |
062 | } |
063 | if (weather2.indexOf( "转" ) > 0 ){ |
064 | //有 |
065 | j_weather_part2 = weather2.split( "转" )[ 0 ]; |
066 | } else { |
067 | j_weather_part2 = weather2; |
068 | } |
069 | if (j_weather_part1.equalsIgnoreCase(j_weather_part2)){ |
070 | j_weather = j_weather_part1; //今天 |
071 | } else { |
072 | j_weather = j_weather_part1 + "转" + j_weather_part2; //今天 |
073 | } |
074 | String m_weather = "" ; |
075 | String m_weather_part1 = "" ; |
076 | String m_weather_part2 = "" ; |
077 | //判断是否有转 |
078 | if (weather2.indexOf( "转" ) > 0 ){ |
079 | //有 |
080 | m_weather_part1 = weather2.split( "转" )[ 1 ]; |
081 | } else { |
082 | m_weather_part1 = weather2; |
083 | } |
084 | if (weather3.indexOf( "转" ) > 0 ){ |
085 | //有 |
086 | m_weather_part2 = weather3.split( "转" )[ 0 ]; |
087 | } else { |
088 | m_weather_part2 = weather3; |
089 | } |
090 | if (m_weather_part1.equalsIgnoreCase(m_weather_part2)){ |
091 | m_weather = m_weather_part1; //今天 |
092 | } else { |
093 | m_weather = m_weather_part1 + "转" + m_weather_part2; //明天 |
094 | } |
095 | //风向风力 |
096 | String j_wind = jso.get( "wind2" ).getAsString(); //今天 |
097 | String m_wind = jso.get( "wind3" ).getAsString(); //明天 |
098 | info.append( "今天:" ).append(j).append( " " ).append(j_weather).append( " " ).append(j_wind).append( "\n" ); |
099 | info.append( "明天:" ).append(m).append( " " ).append(m_weather).append( " " ).append(m_wind).append( "\n" ); |
100 | } |
101 | } |
102 | } catch (Exception e) { |
103 | } |
104 | return info.toString(); |
105 | } |