本文实例讲述了java调用中国天气网api获得天气预报信息的方法。分享给大家供大家参考。具体实现方法如下:
//以冰城哈尔滨为例通过中国天气api调用天气信息
private string getweatherinfo2(){
stringbuilder info = new stringbuilder();
try {
defaulthttpclient httpclient = new defaulthttpclient();
httpget httget = new httpget("http://m.weather.com.cn/data/101050101.html");
responsehandler responsehandler = new basicresponsehandler();
string responsebody = httpclient.execute(httget, responsehandler);
system.out.println(responsebody);
jsonparser jp = new jsonparser();
jsonelement jse = jp.parse(responsebody);
jsonobject jso = jse.getasjsonobject().get("weatherinfo").getasjsonobject();
// string updtime = jso.get("fchh").getasstring();
// if(updtime != null){
// //温度
// string j = jso.get("temp1").getasstring();//今天
// string m = jso.get("temp2").getasstring();//明天
// //天气情况
// string j_weather = jso.get("weather1").getasstring();//今天
// string m_weather = jso.get("weather2").getasstring();//明天
// //风向风力
// string j_wind = jso.get("wind1").getasstring();//今天
// string m_wind = jso.get("wind2").getasstring();//明天
// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
// }
string updtime = jso.get("fchh").getasstring();
if(updtime != null){
if(!updtime.trim().equals("18")){
//温度
string j = jso.get("temp1").getasstring();//今天
string m = jso.get("temp2").getasstring();//明天
//天气情况
string j_weather = jso.get("weather1").getasstring();//今天
string m_weather = jso.get("weather2").getasstring();//明天
//风向风力
string j_wind = jso.get("wind1").getasstring();//今天
string m_wind = jso.get("wind2").getasstring();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}else{
//18
//温度
string temp1 = jso.get("temp1").getasstring();//今天
string temp2 = jso.get("temp2").getasstring();//今天
string temp3 = jso.get("temp3").getasstring();//今天
string j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
string m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
//天气情况
string weather1 = jso.get("weather1").getasstring();
string weather2 = jso.get("weather2").getasstring();
string weather3 = jso.get("weather3").getasstring();
string j_weather = "";
string j_weather_part1 = "";
string j_weather_part2 = "";
//判断是否有转
if(weather1.indexof("转") > 0){
//有
j_weather_part1 = weather1.split("转")[1];
}else{
j_weather_part1 = weather1;
}
if(weather2.indexof("转") > 0){
//有
j_weather_part2 = weather2.split("转")[0];
}else{
j_weather_part2 = weather2;
}
if(j_weather_part1.equalsignorecase(j_weather_part2)){
j_weather = j_weather_part1;//今天
}else{
j_weather = j_weather_part1 + "转" + j_weather_part2;//今天
}
string m_weather = "";
string m_weather_part1 = "";
string m_weather_part2 = "";
//判断是否有转
if(weather2.indexof("转") > 0){
//有
m_weather_part1 = weather2.split("转")[1];
}else{
m_weather_part1 = weather2;
}
if(weather3.indexof("转") > 0){
//有
m_weather_part2 = weather3.split("转")[0];
}else{
m_weather_part2 = weather3;
}
if(m_weather_part1.equalsignorecase(m_weather_part2)){
m_weather = m_weather_part1;//今天
}else{
m_weather = m_weather_part1 + "转" + m_weather_part2;//明天
}
//风向风力
string j_wind = jso.get("wind2").getasstring();//今天
string m_wind = jso.get("wind3").getasstring();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}
}
} catch (exception e) {
}
return info.tostring();
}
希望本文所述对大家的java程序设计有所帮助。
希望与广大网友互动??
点此进行留言吧!