首页 > 学院 > 开发设计 > 正文

[学习笔记]如何调用百度天气api,学习自百度api

2019-11-11 06:50:45
字体:
来源:转载
供稿:网友

使用的是struts框架,调用结果返回出weatherResult【json格式】,再传到前台

public String getWeather() {

System.out.PRintln("hello weather");BufferedReader reader = null;StringBuffer sbf = new StringBuffer();String httpUrl = "http://apis.baidu.com/apix/idauth/idauth" + "?" + "name=刘坤&cardno=320921199108025071";try {URL url = new URL(httpUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");// 填入apikey到HTTP headerconnection.setRequestProperty("apikey","3286167d75112ab25de8936c7a270b35");connection.connect();InputStream is = connection.getInputStream();reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));String strRead = null;while ((strRead = reader.readLine()) != null) {sbf.append(strRead);sbf.append("/r/n");}reader.close();weatherResult = sbf.toString();} catch (Exception e) {e.printStackTrace();}System.out.println(weatherResult);return SUCCESS;

}

json数据示例:

{    "HeWeather data service 3.0": [{        "status": "ok",    //接口状态,参考http://www.heweather.com/documents/api         "basic": { //基本信息            "city": "故宫博物院",  //景点名称            "cnty": "中国",  //国家            "id": "CN10101010018A",  //景点ID,参见http://www.heweather.com/documents/cn-attractions-list            "lat": "116.39",  //城市维度            "lon": "39.91",  //城市经度            "update": { //更新时间,景点天气每天更新三次,分别为8、15、18点                "loc": "2015-07-02 08:10",  //当地时间,如无特殊说明,以下时间均为当地时间                "utc": "2015-07-02 00:10" //UTC时间            }        },        "daily_forecast": [ //7天预报        {            "date": "2015-10-10",  //预报日期,第一天            "astro": { //天文数值                  "sr": "06:19",  //日出时间                "ss": "17:43" //日落时间            },            "cond": { //天气状况,天气状况代码和图标,参考http://www.heweather.com/documents/condition-code                "code_d": "101",  //白天天气状况代码                "code_n": "101",  //夜间天气状况代码                "txt_d": "多云",  //白天天气状况描述                "txt_n": "多云" //夜间天气状况描述            },            "tmp": { //温度                "max": "16",  //最高温度                "min": "10" //最低温度            },            "wind": {   //风力风向                "dir": "北风",  //风向                "sc": "3-4"  //风力            }        },       {......}//第二到第七天重复以上内容,略        ]    }  ]}
本本参考http://apistore.baidu.com/apiworks/servicedetail/880.html,勿喷


上一篇:2.6

下一篇:ASP.NET Cache缓存

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表