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
原创粉丝点击