[android开发]天气预报项目的开发之数据接口与网络请求-1


先看界面: 


天气首页:

[android开发]天气预报项目的开发之数据接口与网络请求-1_第1张图片





actionbar:

[android开发]天气预报项目的开发之数据接口与网络请求-1_第2张图片



设置天气城市页面:

[android开发]天气预报项目的开发之数据接口与网络请求-1_第3张图片



整体界面比较简单, 将就吧..


1.网络请求接口:

网络接口用的是百度提供的. 返回的数据是json格式.

url: http://api.map.baidu.com/telematics/v3/weather

参数1:  location:天气城市,比如'广州';    

参数2:  output: 输出格式, 这里固定用 'json'

参数3:  ak: 这是百度服务的api key, 需要去 百度开发者中心-开发者服务管理去创建一个工程, 得到api key即可, 如下图:

[android开发]天气预报项目的开发之数据接口与网络请求-1_第4张图片



看看请求接口返回的数据:

访问地址: http://api.map.baidu.com/telematics/v3/weather?ak=你的api_key&output=json&location=广州

返回的数据:

[android开发]天气预报项目的开发之数据接口与网络请求-1_第5张图片







接下来就可以解析啦~



2.  我们先定义两个javaBean来装这些数据:

1. 存放请求的整个结果的Result类:

public class Result implements Serializable{
	public String currentCity;
	public String pm25;
	public ArrayList weather_data = new ArrayList();
	
	public String currentDegree;
	public Weather today;
	public Weather tomorrow;
	
	public Integer code = 200;
	
	
	public static final int CODE_OK = 200;
	public static final int CODE_NO_SUCH_CITY = 1;
	public static final int CODE_NETWORK_ERROR = 2;
	public static final int CODE_PARSE_JSON_ERROR = 3;
}


2.然后是存放每一天的天气Weather类:

public class Weather implements Serializable {
	//周三 02月11日 (实时:13℃)   周四
	public String date;
	
	//多云
	public String weather;
	
	//微风
	public String wind;
	
	//22 ~ 9℃
	public String temperature;
	
	
	public String tickerText;
}



好了, 接下来就开始请求网络并解析数据咯, 下面的post方法, 这里使用了async-http-client开源库, 使用方法跟httpclient基本一样, 只不过他封装成了异步请求:

public static void post(final Context context, String location, final OnCallBack onCallBack) {
		RequestParams params = new RequestParams();
		params.put("location", location);
		params.put("output", "json");
		params.put("ak", ak);
		client.get(context, url, params, new JsonHttpResponseHandler("utf-8") {
			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
				Result bean = new Result();
				try {
					String status = response.getString("status");
					if ("success".equals(status)) {
						JSONArray results = response.getJSONArray("results");
						JSONObject result = results.getJSONObject(0);
						bean = ConvertUtil.json2Bean(result, Result.class);
						try {
							Weather w = bean.weather_data.get(0);
							bean.today = w;
							bean.tomorrow = bean.weather_data.get(1);
							bean.tomorrow.date = "明天";

							try {
								bean.currentDegree = w.date.substring(w.date.indexOf("(") + 4, w.date.lastIndexOf(")"));
							} catch (Exception e) {
								bean.currentDegree = "无";
							}
							try {
								if (w.date.startsWith("周"))
									w.date = "今天";
								else
									w.date = w.date.substring(0, w.date.indexOf(" "));
							} catch (Exception e) {
							}
							WeatherUtil.setTickerText(bean);

						} catch (Exception e) {
						}
						onCallBack.onCallBack(bean);
					} else {
						bean.code = Result.CODE_NO_SUCH_CITY;
						onCallBack.onCallBack(bean);
					}
				} catch (Exception e) {
					bean.code = Result.CODE_PARSE_JSON_ERROR;
					onCallBack.onCallBack(bean);
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable e) {
				Result bean = new Result();
				bean.code = Result.CODE_NETWORK_ERROR;
				onCallBack.onCallBack(bean);
			}

		});
	}


上面方法的第三个参数是我自己定义的一个回调接口, 当异步请求成功/失败的时候就会调用接口内的方法, 并把Result参数传进去供你操作.

public interface OnCallBack {
	public void onCallBack(T t);
}




好了, 网络请求就写到这里.












你可能感兴趣的:([android开发]天气预报项目的开发之数据接口与网络请求-1)