java ip调天气预报接口_java调天气预报接口

国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.html http://m.weather.com.cn/data/101010100.html 对于第三个接口,返回信息比较全面,也是以json格式提供

1、java调用天气预报接口,参考

http://g.kehou.com/t1029846752.html,现在比如我访问成都的天气预报,则访问地址

http://m.weather.com.cn/data/101270101.html,就可一得到一串json数据。我们就可以对这串进行解析

如果用ajax直接访问是最简单的一种方式,但涉及到一个跨域的问题,转自:http://blog.csdn.net/xiaanming/article/details/16871117 转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢! 相信大家在平常的开发中,对网络的操作用到HTTP协议比至今都还没搞懂啊,跪求大神

java

现在用java解析就变得比较简单,上代码:

@GET

@Path("/get")

@Produces(MediaType.APPLICATION_JSON)

public String getWeather() throws MalformedURLException, IOException,

JSONException {

try {

HttpURLConnection huc = (HttpURLConnection) new URL(

"http://m.weather.com.cn/data/101270101.html")

.openConnection();

huc.setRequestMethod("GET");

huc.setUseCaches(true);

huc.connect();

InputStream is = huc.getInputStream();

BufferedReader reader = new BufferedReader(

new InputStreamReader(is));

StringBuffer temp = new StringBuffer();

String str;

while ((str = reader.readLine()) != null) {

temp.append(str + "\n");

}

System.out.println(temp.toString());

is.close();

reader.close();

return temp.toString();

} catch (Exception e) {

e.printStackTrace();

} finally{

}

return null;

}

2、这里用到的是restful,无所谓用什么,我们用ajax调用到这个方法时就会返回一串json数据到前台如代码,

function getWeather() {

$.ajax({

url : 'rest/weather/get',

type : 'GET',

success : function(meta) {

var w = meta.weatherinfo;

$("#w1").empty().append("今天:"+w.weather1+" "+w.temp1+" d%22+w.img1+%22.gif");

$("#w2").empty().append("明天:"+w.weather2+" "+w.temp2+" d%22+w.img3+%22.gif");

$("#w3").empty().append("后天:"+w.weather3+" "+w.temp3+" d%22+w.img5+%22.gif");

}

});

}

ajax可能就是解析一个json数据。。。

注:img中装的是天气预报提供的表示各种天气的图片,参考

http://g.kehou.com/t1029846752.html 讲的很清楚。

差不多了。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值