一、描述步骤
1.定位api:http://pv.sohu.com/cityjson?ie=utf-8
例子:var returnCitySN = {"cip": "113.66.222.99", "cid": "440100", "cname": "广东省广州市"};
2.获取天气api:(中央气象台接口)共3个步骤
步骤1:http://www.nmc.cn/rest/province (获取全国省份的代码)例如:广东省:AGD
步骤2:http://www.nmc.cn/rest/province/AGD (获取广东省的所有城市代码)例如:广州市:59287
步骤3:http://www.nmc.cn/rest/weather?stationid=59287(北京七天的白天和夜晚的天气信息)
3.参考资料
中英天气对照:https://wenku.baidu.com/view/2c211b48b52acfc788ebc92a.html
天气预报图例:http://www.weather.com.cn/static/html/legend.shtml
中央气象台的API获取天气:(缺点没有天气图标)
http://flash.weather.com.cn/wmaps/xml/china.xml
http://flash.weather.com.cn/wmaps/xml/beijing.xml
二、代码
1.保存天气信息类
public class WeatherForm {
//天气描述
private String weather;
//图标
private int img;
//省
private String provice;
//城市
private String city;
//省市
private String location;
public WeatherForm(){
}
public WeatherForm(String weather,int img, String provice,
String city, String location) {
super();
this.weather = weather;
this.img = img;
this.provice = provice;
this.city = city;
this.location = location;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getProvice() {
return provice;
}
public void setProvice(String provice) {
this.provice = provice;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "WeatherForm [weather=" + weather
+ ", img=" + img
+ ", provice=" + provice + ", city=" + city
+ ", location=" + location
+"]";
}
}
2.调用中央气象台http接口,获取天气信息jons包,解析分解需要的信息
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.json.JSONObject;
import org.json.JSONException;
import org.json.JSONArray;
import android.util.Log;
public class WeatherQueryManage {
private static String TAG = "WeatherQueryManage";
public WeatherForm[] weatherquery() {
WeatherForm[] WF = new WeatherForm[3];
//http://m.weather.com.cn/data/101070101.html
//String URL = "http://m.weather.com.cn/data/"+CityId+".html";
//http://m.weather.com.cn/atad/101070101.html
//String URL="http://www.weather.com.cn/weather/101070101.shtml";
//String URL="http://m.weather.com.cn/atad/"+CityId+".html";
//String URL="http://m.weather.com.cn/atad/101070101.html";
//String URL="http://www.weather.com.cn/data/cityinfo/101010100.html";
//String URL_img3 = "http://image.nmc.cn/assets/img/w/40x40/4/3.png";
//String URL_img_url = "http://image.nmc.cn/assets/img/w/40x40/4/";
//String URL_img_png = ".png"; String URL = "http://www.nmc.cn/rest/weather?stationid=54511";
String URL2 = "http://flash.weather.com.cn/wmaps/xml/beijing.xml";
String URL3 = "http://flash.weather.com.cn/wmaps/xml/china.xml";
String URL_weather = "http://www.nmc.cn/rest/weather?stationid=";
String URL_location = "http://pv.sohu.com/cityjson?ie=utf-8";
String Weather_Result="";
String Location_Result="";
String city_code = "";
String location = "";
WeatherForm weaf = new WeatherForm();
WF[0]=weaf;
location = getLocation(WF);
Log.v(TAG," test 121212");
city_code = getCitycode(location,WF);
if(city_code == null || "".equals(city_code)){
Log.v(TAG," test city_code = "+city_code);
return null;
}
HttpGet httpRequest = new HttpGet(URL_weather+city_code);
//HttpGet httpRequest_location = new HttpGet(URL_location);
Log.v(" test"," URL_weather+city_code:"+URL_weather+city_code);
//Log.v(" test"," URL_location:"+URL_location);
// 获得HttpResponse对象
try {
Log.v(" test",