[学习笔记]如何调用百度天气api,学习自百度api
2017-02-05 19:06
453 查看
使用的是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 header
connection.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数据示例:
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 header
connection.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/s 88e1 ervicedetail/880.html,勿喷
相关文章推荐
- QT学习笔记——如何调用父类的构造函数
- .NET调用百度天气api经验
- ArcGIS API for JavaScript 4.2学习笔记[23] 没有地图如何进行查询?【FindTask类的使用】
- HIVE学习笔记:HiveServer2,调用HIVE的JavaAPI
- PHP学习笔记:利用百度api实现手机归属地查询
- PHP调用百度天气接口API
- 【学习笔记】【Cocos2d-x Lua脚本开发】如何使用Lua脚本调用自定义类
- 学习Altas 笔记[js调用重载的方法出错,如何处理]
- 学习Altas 笔记[js调用重载的方法出错,如何处理]
- .Net语言 APP开发平台——Smobiler学习日志:如何调用API进行短信发送
- 百度 地点输入提示服务 API 学习笔记
- Android——调用百度天气API实现天气预报
- 如何调用百度的api
- Lua学习笔记(一) vs2013如何在C++中调用Lua(修正版)
- [iphone 开发学习笔记]Object-C和C语言最大的区别也是最大的扩展-----消息(即如何调用一个对象中的函数)
- 如何学习百度API
- ArcGIS API for javascript开发笔记(六)——REST详解及如何使用REST API调用GP服务
- ArcGIS API for javascript开发笔记(六)——REST详解及如何使用REST API调用GP服务
- 百度API学习笔记1
- Java学习笔记 Java调用Win32 API控制鼠标活动范围