這兩天在做一個天氣預報的程式,在網上找了找資料,最後決定根據中國天氣網提供的介面來編寫,點擊一下就會發現http://m.weather.com.cn/data/101070101.html,返回的是JSON格式,使用起來非常方便。
下面是部分主要代碼:
首先先寫一個天氣的Form-WeatherForm:主要有城市名、城市編號(就是網址中的9位元字)、當前日期、溫度、風力等。
package com.example.weatherform;public class WeatherForm {/**城市名*/private String name;/**城市編號*/private String id;/**當前日期*/private String ddate;/**星期*/private String week;/**溫度*/private String temp;/**天氣描述*/private String weather;/**風力*/private String wind;/**風向*/private String fx;public WeatherForm(){}/** * 構造方法 * @param name * @param id * @param ddate * @param week * @param temp * @param weather * @param wind * @param fx */public WeatherForm(String name, String id, String ddate, String week,String temp, String weather, String wind, String fx) {super();this.name = name;this.id = id;this.ddate = ddate;this.week = week;this.temp = temp;this.weather = weather;this.wind = wind;this.fx = fx;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getDdate() {return ddate;}public void setDdate(String ddate) {this.ddate = ddate;}public String getWeek() {return week;}public void setWeek(String week) {this.week = week;}public String getTemp() {return temp;}public void setTemp(String temp) {this.temp = temp;}public String getWeather() {return weather;}public void setWeather(String weather) {this.weather = weather;}public String getWind() {return wind;}public void setWind(String wind) {this.wind = wind;}public String getFx() {return fx;}public void setFx(String fx) {this.fx = fx;}@Overridepublic String toString() {return "WeatherForm [name=" + name + ", id=" + id + ", ddate=" + ddate+ ", week=" + week + ", temp=" + temp + ", weather=" + weather+ ", wind=" + wind + ", fx=" + fx + "]";}}
下面是查詢天氣的主要介面:
package com.example.weatherquery;import com.example.weatherform.WeatherForm;public interface WeatherQueryManage {/** * 查詢天氣方法 * @param CityId 對應城市的id * @return 暫時返回3天的天氣資料 */public WeatherForm[] weatherquery(String CityId);}
下面是實現該方法的主要類:
package com.example.weatherquery;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.json.JSONException;import org.json.JSONObject;import com.example.weatherform.WeatherForm;import android.util.Log;public class WeatherQueryManageImpl implements WeatherQueryManage {private final String TAG = "message";@Overridepublic WeatherForm[] weatherquery(String CityId) {WeatherForm[] WF = new WeatherForm[3];//http://m.weather.com.cn/data/101070101.htmlString URL = "http://m.weather.com.cn/data/"+CityId+".html";String Weather_Result="";HttpGet httpRequest = new HttpGet(URL);// 獲得HttpResponse對象try {HttpClient httpClient = new DefaultHttpClient();HttpResponse httpResponse = httpClient.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 取得返回的資料Weather_Result = EntityUtils.toString(httpResponse.getEntity());}} catch (Exception e) {Log.i(TAG, e.toString());WF[0] = new WeatherForm();WF[1] = new WeatherForm();WF[2] = new WeatherForm();return WF;}//以下是對返回JSON資料的解析if(null!=Weather_Result&&!"".equals(Weather_Result)){try {JSONObject JO = new JSONObject(Weather_Result).getJSONObject("weatherinfo");for(int i=0;i<WF.length;i++){WeatherForm weaf = new WeatherForm();//3個日期暫時都存放一天的weaf.setName(JO.getString("city"));weaf.setDdate(JO.getString("date_y"));weaf.setWeek(JO.getString("week"));weaf.setTemp(JO.getString("temp"+(i+1)));weaf.setWind(JO.getString("wind"+(i+1)));weaf.setWeather(JO.getString("weather"+(i+1)));WF[i]=weaf;}} catch (JSONException e) {Log.i(TAG, e.toString());WF[0] = new WeatherForm();WF[1] = new WeatherForm();WF[2] = new WeatherForm();return WF;}}return WF;}}
這樣前台傳送一個城市的編號,在後台即可返回相應城市3天內的資訊。在中國天氣網返回的資訊中有很多,這裡只提煉的一少部分。
原始碼可以在這裡下載,包括城市-城市編號的參照表在程式的Assets資源夾中。點擊下載源碼:http://download.csdn.net/detail/best198706/6004347