一、描述步驟
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"," HttpClient 1111111111111111:");
HttpClient httpClient = new DefaultHttpClient();
Log.v(" test"," HttpResponse 222222222222222");
HttpResponse httpResponse = httpClient.execute(httpRequest);
Log.v(" test"," HttpResponse 222222222222222"+httpResponse);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得傳回的資料
Log.v(" test"," httpResponse.getStatusLine().getStatusCode():"+httpResponse.getStatusLine().getStatusCode());
Weather_Result = EntityUtils.toString(httpResponse.getEntity());
//Log.v(" test"," Weather_Result:"+Weather_Result);
}
//HttpClient httpClient_location = new DefaultHttpClient();
//HttpResponse httpResponse_location = httpClient_location.execute(httpRequest_location);
//if (httpResponse_location.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得傳回的資料
// Log.v(" test"," httpResponse_location.getStatusLine().getStatusCode():"+httpResponse_location.getStatusLine().getStatusCode());
// Location_Result = EntityUtils.toString(httpResponse_location.getEntity());
// Log.v(" test"," Location_Result:"+Location_Result);
//}
} catch (Exception e) {
Log.v(" test err ++++++",""+e);
WF[0] = new WeatherForm();
WF[1] = new WeatherForm();
WF[2] = new WeatherForm();
return null;
}
//以下是對傳回JSON資料的解析
if( null!=Weather_Result && !"".equals(Weather_Result)){
try {
//Log.v(" test "," 00000 "+Location_Result);
JSONObject JO = new JSONObject(Weather_Result).getJSONObject("data");
//int index = Location_Result.indexOf("{");
//String city = new JSONObject(Location_Result.substring(index)).getString("cname");
//Log.v(" test "," 111111 "+city);
//
//Log.v(" test"," Weather__000"+JO.getString("real"));
JSONArray jsonArray =new JSONArray(JO.getJSONObject("predict").getString("detail"));
for(int i=0;i<jsonArray.length();i++){
//Log.v(" test"," Weather__11"+i+jsonArray.getJSONObject(i));
}
//Log.v(" test"," Weather__222"+jsonArray.getJSONObject(0).getString("day"));
Log.v(" test"," Weather__iiii"+jsonArray.getJSONObject(0));
String day_weather = jsonArray.getJSONObject(0).getJSONObject("day").getJSONObject("weather").getString("info");
String night_weather = jsonArray.getJSONObject(0).getJSONObject("night").getJSONObject("weather").getString("info");
int img_day = Integer.parseInt(jsonArray.getJSONObject(0).getJSONObject("day").getJSONObject("weather").getString("img"));
int img_night = Integer.parseInt(jsonArray.getJSONObject(0).getJSONObject("night").getJSONObject("weather").getString("img"));
Log.v(" test"," day_weather="+day_weather);
Log.v(" test"," night_weather="+night_weather);
//Log.v(" test"," city=" +city);
Log.v(" test"," img_day=" +img_day+" img_night="+img_night);
Log.v(" test"," d&n "+day_weather.contains("9")+night_weather.contains("9"));
//weaf.setCity(city);
//weaf.setDdate(JO.getString("date_y"));
//weaf.setWeek(JO.getString("week"));
//weaf.setTemp(JO.getString("temp"));
//weaf.setWind(JO.getString("wind"));
if( !day_weather.contains("9") && !night_weather.contains("9") ) {
if(day_weather.equals(night_weather))
WF[0].setWeather(day_weather);
else
WF[0].setWeather(day_weather + "轉" + night_weather);
WF[0].setImg(img_day);
}
else if( day_weather.contains("9") && !night_weather.contains("9") ){
WF[0].setWeather(night_weather);
WF[0].setImg(img_night);
}
else if( !day_weather.contains("9") && night_weather.contains("9") ){
WF[0].setWeather(day_weather);
WF[0].setImg(img_day);
}
else {
Log.v(" test" ,"dayweather:"+day_weather+" nightweather"+night_weather);
WF[0].setWeather("");
WF[0].setImg(99);
}
//Log.v(" test"," WF[0]:"+WF[0]);
} catch (JSONException e) {
WF[0] = new WeatherForm();
WF[1] = new WeatherForm();
WF[2] = new WeatherForm();
return null;
}
}
Log.v(" test"," WF[0]:"+WF[0]);
return WF;
}
private String getLocation(WeatherForm[] WF){
String URL_location = "http://pv.sohu.com/cityjson?ie=utf-8";
String location = "";
String Location_Result="";
HttpGet httpRequest_location = new HttpGet(URL_location);
Log.v(" test"," URL_location:"+URL_location);
// 獲得HttpResponse對象
try {
HttpClient httpClient_location = new DefaultHttpClient();
HttpResponse httpResponse_location = httpClient_location.execute(httpRequest_location);
if (httpResponse_location.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得傳回的資料
Log.v(" test"," httpResponse_location.getStatusLine().getStatusCode():"+httpResponse_location.getStatusLine().getStatusCode());
Location_Result = EntityUtils.toString(httpResponse_location.getEntity());
//Log.v(" test"," Location_Result:"+Location_Result);
}
} catch (Exception e) {
Log.v(" test getLocation err ++++++",""+e);
return null;
}
//以下是對傳回JSON資料的解析
if( null!=Location_Result && !"".equals(Location_Result)){
try {
//Log.v(" test "," 00000 "+Location_Result);
int index = Location_Result.indexOf("{");
location = new JSONObject(Location_Result.substring(index)).getString("cname");
Log.v(" test"," location=" +location);
WF[0].setLocation(location);
} catch (JSONException e) {
Log.v(" test"," getLocation: err "+e);
return null;
}
}
Log.v(" test"," getLocation() done ==="+location);
return location;
}
private String getCitycode(String location,WeatherForm[] WF)
{
Log.v(" test"," getCitycode=====");
if(null == location || "".equals(location)){
Log.v(" test"," location is null " +location);
return null;
}
String URL_province = "http://www.nmc.cn/rest/province";
String URL_city = "http://www.nmc.cn/rest/province/";
String provice_Result = "";
String provice_code = "";
String provice_name = "";
String city_Result = "";
String city_code = "";
String city_name = "";
HttpGet httpRequest = new HttpGet(URL_province);
Log.v(" test"," URL_province:"+URL_province);
// 獲得HttpResponse對象
try {
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得傳回的資料
Log.v(" test"," httpResponse.getStatusLine().getStatusCode():"+httpResponse.getStatusLine().getStatusCode());
provice_Result = EntityUtils.toString(httpResponse.getEntity());
//Log.v(" test"," provice_Result:"+provice_Result);
}
} catch (Exception e) {
Log.v(" test provice_Result err ++++++",""+e);
return null;
}
//以下是對傳回JSON資料的解析
if( null!=provice_Result && !"".equals(provice_Result)){
try {
JSONArray jsonArray =new JSONArray(provice_Result);
for(int i=0;i<jsonArray.length();i++){
//Log.v(" test"," provice__11"+i+jsonArray.getJSONObject(i));
if(location.contains(jsonArray.getJSONObject(i).getString("name"))){
provice_name = jsonArray.getJSONObject(i).getString("name");
Log.v(" test"," provice_name="+provice_name);
provice_code = jsonArray.getJSONObject(i).getString("code");
Log.v(" test"," provice_code="+provice_code);
WF[0].setProvice(provice_name);
break;
}
}
} catch (JSONException e) {
Log.v(" test"," get provice_code err:");
return null;
}
}
if( null!=provice_code && !"".equals(provice_code) ){
HttpGet httpRequest_city = new HttpGet(URL_city+provice_code);
Log.v(" test"," URL_city+provice_code:"+URL_city+provice_code);
// 獲得HttpResponse對象
try {
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest_city);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得傳回的資料
Log.v(" test"," httpResponse.getStatusLine().getStatusCode():"+httpResponse.getStatusLine().getStatusCode());
city_Result = EntityUtils.toString(httpResponse.getEntity());
//Log.v(" test"," city_Result:"+city_Result);
}
} catch (Exception e) {
Log.v(" test get city_Result err ++++++",""+e);
return null;
}
//以下是對傳回JSON資料的解析
if( null!=city_Result && !"".equals(city_Result)){
try {
JSONArray jsonArray =new JSONArray(city_Result);
for(int i=0;i<jsonArray.length();i++){
//Log.v(" test"," city__11"+i+jsonArray.getJSONObject(i));
if(location.contains(jsonArray.getJSONObject(i).getString("city"))){
city_name = jsonArray.getJSONObject(i).getString("city");
Log.v(" test"," city_name="+city_name);
city_code = jsonArray.getJSONObject(i).getString("code");
Log.v(" test"," city_code="+city_code);
WF[0].setCity(city_name);
break;
}
}
} catch (JSONException e) {
Log.v(" test"," get provice_code err:");
return null;
}
}
}
return city_code;
}
}
3.調用接口類擷取天氣
public void getweather()
{
Log.v(TAG," test getweather");
new Thread(new Runnable() {
@Override
public void run() {
Log.v(TAG," test getweather run");
//建立傳回的天氣資訊對象,是一個對象數組,用來放3天的天氣
WeatherForm[] WF = new WeatherForm[3];
//查詢天氣,傳回3天的天氣資訊
Log.v(TAG," test getweather::::");
WeatherQueryManage WQM = new WeatherQueryManage();
int count = 1;
while(!haveInternet())
{
Log.v(TAG," test count="+count);
if(count++>10) {
Log.v(TAG," test network disconnect!");
return;
}
try{
Thread.sleep(100);
}catch(Exception e){
Log.e(TAG," test sleep err! "+e);
}
}
Log.v(TAG," test haveInternet!");
WF = WQM.weatherquery();
if(null!=WF && WF.length>=1){
Log.v(TAG," test getweather: WF.length="+WF.length);
Log.v(TAG," test weather:"+WF[0].getWeather());
String weather = WF[0].getWeather();
Log.v(TAG," test weather send :"+weather);
Message message = new Message();
message.what = 99;
message.obj = WF[0].getWeather();
message.arg1 = WF[0].getImg();
Handler.sendMessage(message);
}
}
}).start();
}
參考路徑:
擷取目前城市:https://blog.csdn.net/Frederick_Fung/article/details/107718091?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162583091916780366597455%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162583091916780366597455&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-27-107718091.pc_search_result_before_js&utm_term=%E7%AE%80%E5%8D%95%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E5%9F%8E%E5%B8%82%E6%8E%A5%E5%8F%A3%E5%85%8D%E8%B4%B9&spm=1018.2226.3001.4187
擷取天氣詳細步驟:(中央氣象台api)https://blog.csdn.net/u014436243/article/details/113625266?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162581684716780261942329%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162581684716780261942329&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-4-113625266.pc_search_result_before_js&utm_term=%E8%8E%B7%E5%8F%96%E5%BC%80%E6%BA%90%E5%A4%A9%E6%B0%94%E4%BF%A1%E6%81%AF&spm=1018.2226.3001.4187