Android基于百度api的天气预报
来源:互联网 发布:seo黑帽工具 编辑:程序博客网 时间:2024/05/16 12:22
这里主要介绍的是如何使用百度api获取天气数据,首先我们需要去申请一个ak,申请地址是http://lbsyun.baidu.com/apiconsole/key。
申请步骤如下:点击创建应用—>
,提交之后就会出现ak。
获取天气数据方法:
private void getData() {cityNameOrCoord = "北京市";final WeatherEntity weather = new WeatherEntity();HttpUtils httpUtils = new HttpUtils(60000);String ak = "Em8bLaBbUcQ1CnKjcYxbSO0UH8hwugCu";String url = "";try {url = "http://api.map.baidu.com/telematics/v3/weather?"+ "location=" +URLEncoder.encode(cityNameOrCoord,"UTF-8") + "&output=json&ak=" + ak+"&mcode="+"55:42:F5:75:32:28:A8:5D:30:04:98:C7:4D:12:65:B1:7E:71:F9:9F;com.example.weatherdemo";} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {@Overridepublic void onFailure(HttpException arg0, String arg1) {Toast.makeText(MainActivity.this, "请查看是否已经开启网络", 0).show();}@Overridepublic void onSuccess(ResponseInfo<String> arg0) {try {JSONObject json = new JSONObject(arg0.result);if (json.getString("status").equals("success")) {JSONArray array = json.getJSONArray("results");JSONObject jsonObject = array.getJSONObject(0);weather.current_city = jsonObject.getString("currentCity");weather.pm2_5 = jsonObject.getString("pm25");JSONArray weatherJson = jsonObject.getJSONArray("weather_data");weather.weatherDateList = new ArrayList<WeatherDataEntity>();for (int i = 0; i < 3; i++) {JSONObject js = weatherJson.getJSONObject(i);WeatherDataEntity weatherData = new WeatherDataEntity();String date = js.getString("date");if (i == 0) {String week = date.substring(0, 2);weatherData.cur_week = week;weatherData.cur_date = date.substring(3, 9);int startIndex = date.indexOf(":") + 1;int endIndex = date.indexOf(")");weather.current_temp = date.substring(startIndex, endIndex);} else {weatherData.cur_week = date;}weatherData.current_image_day = js.getString("dayPictureUrl");weatherData.current_image_night = js.getString("nightPictureUrl");String str = js.getString("weather");weather.state_weather = str;if (str.contains("转")) {String[] splitStr = str.split("转");weatherData.state_weather_day = splitStr[0];weatherData.state_weather_night = splitStr[1];} else {weatherData.state_weather_day = str;weatherData.state_weather_night = str;}weatherData.wind = js.getString("wind");weatherData.high_low_temp = js.getString("temperature");weather.weatherDateList.add(weatherData);}setData(weather);}} catch (JSONException e) {e.printStackTrace();}}});}
mcode就是SHA1,想要获取城市天气的话动态改变cityNameOrCoord的值即可。这里有个setData()方法,就是设置数据,在这里就不贴代码了。
用到的实体类:
public class WeatherEntity {public String current_temp;public String current_city;public String pm2_5;public String state_weather;public String quality_air;public String update_time;public ArrayList<WeatherDataEntity> weatherDateList;}
public class WeatherDataEntity {public String cur_date;public String cur_week;public String current_image_day;public String current_image_night;public String state_weather_day;public String state_weather_night;public String wind;public String high_low_temp;}需要添加image-loader、xutils jar包,清单文件里加联网权限。还有在使用imageloader之前需要对其进行初始化:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();// 开始构建ImageLoader.getInstance().init(config);
源码下载地址:http://download.csdn.net/detail/xkyh941/9630732
0 0
- Android基于百度api的天气预报
- Android 基于百度的天气预报
- 基于百度地图API实现的地图形式天气预报【一】
- 基于百度地图API实现的地图形式天气预报【二】
- 基于百度地图API实现的地图形式天气预报
- 基于百度地图API实现的地图形式天气预报【二】
- 百度天气预报API的使用
- Android基于百度API的定位功能
- 百度 天气预报API
- Android——天气预报(MVP、Gson和天气、百度地图Api的练习)
- Android上实现一个简单的天气预报APP(十四) 使用百度API定位城市
- Android——调用百度天气API实现天气预报
- 百度天气预报API的使用(java版本)
- 使用百度天气API实现自己的天气预报
- 基于百度地图API的Android公交换乘导航
- Android中Yahoo天气预报API的使用
- 基于百度地图的定位实现天气预报查询
- 微信api + 百度api 开发天气预报
- 解决Qt5Core.dll丢失
- 利用path的lineto方法绘制一个三角形
- EditText使用属性详解
- Mysql 语句执行顺序
- 在win7,win10下将CapsLock按键变成esc
- Android基于百度api的天气预报
- 4-String 字符串
- 【Android】选项菜单optionsMenu
- Mysql 多表查询详解
- 超详细的GDB命令
- HDU 5881 Tea(思维)——2016 ACM/ICPC Asia Regional Qingdao Online
- 合并两个数组为一个数组,并且对新数组进行排序
- 差分约束系统
- c笔试常考知识汇总