java获取天气预报_java调用中国天气网api获得天气预报信息的方法

本文展示了如何使用Java通过中国天气网API获取哈尔滨的天气预报信息,包括温度、天气情况和风向风力。示例代码详细解释了如何解析JSON响应并提取相关天气数据。
摘要由CSDN通过智能技术生成

本文实例讲述了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程序设计有所帮助。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值