您的位置:首页 > 移动开发 > Android开发

Android实现天气预报(中国天气网)

2016-06-15 21:25 417 查看
Android实现天气预报(中国天气网)
 
这两天在做一个天气预报的程序,在网上找了找资料,最后决定根据中国天气网提供的接口来编写,
  点击一下就会发现http://m.weather.com.cn/data/101070101.html
  返回的是JSON格式,使用起来非常方便。
  下面是部分主要代码:
  首先先写一个天气的Form-WeatherForm:
  主要有城市名、城市编号(就是网址中的9位数字)、当前日期、温度、风力等。
  java代码
    packagecom.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() {

c538
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;
}
@Override
public String toString() {
return "WeatherForm [name=" + name + ", id=" + id +", ddate=" + ddate
+ ", week=" + week + ", temp=" + temp + ", weather="+ weather
+ ", wind=" + wind + ", fx=" + fx + "]";
}
}
  下面是查询天气的主要接口:
  java代码
    packagecom.example.weatherquery;
 
import com.example.weatherform.WeatherForm;
public interface WeatherQueryManage {
/**
*
查询天气方法
* @param CityId
对应城市的id
* @return
暂时返回3天的天气数据
*/
public WeatherForm[] weatherquery(String CityId);
}
复制代码
  下面是实现该方法的主要类:
  java代码
    packagecom.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";
@Override
public WeatherForm[] weatherquery(String CityId) {
WeatherForm[] WF = new WeatherForm[3];
//http://m.weather.com.cn/data/101070101.html
String 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 
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: