標籤:android 城市代碼獲得 天氣情況獲得 本地ip獲得 本地城市名獲得
今天項目新添加了得到本地當天的溫度,濕度,pm2.5的值的需求,研究了下,記下勞動成果,為碼農少走彎路做貢獻。
思路如下:
1.得到手機的外網ip(http://ip-api.com/json)
2.得到本地資訊:省份和城市名(http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=)
3.根據省份城市名拿到城市代碼(https://code.csdn.net/snippets/621277/master/blog_20150317_1_5052706/raw)
4.根據城市代碼得到天氣情況(http://wthrcdn.etouch.cn/WeatherApi?citykey=)
當然天氣情況裡面有很多這裡只拿溫度,濕度和pm2.5
具體代碼如下:
package com.smart.model;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;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.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;public class WeatherCondition { public static String getIpUrl = "http://ip-api.com/json"; public String getAddress = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="; public String cityCode = "https://code.csdn.net/snippets/621277/master/blog_20150317_1_5052706/raw"; public String weatherUrl = "http://wthrcdn.etouch.cn/WeatherApi?citykey=";// http://m.weather.com.cn/mweather/101280601.shtml private String locaInfo; public SearchWeatherListner weatherListner; private String city ; public interface SearchWeatherListner{ public void weatherInfo(String city,String tem,String shidu,String pm); public void error(Exception e); } public WeatherCondition(SearchWeatherListner weatherListner) {super();this.weatherListner = weatherListner;}public void getWebIp(final String urlStr) { new Thread() { public void run() { String strForeignIP = ""; try { URL url = new URL(urlStr); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String s = ""; StringBuffer sb = new StringBuffer(""); while ((s = br.readLine()) != null) { sb.append(s + "\r\n"); } br.close(); String webContent = ""; webContent = sb.toString(); System.out.println("webContent:"+webContent); if(urlStr.equals(getIpUrl)){//1 得到ip getIp(webContent); }else if(urlStr.equals(cityCode)){//3 得到所有cityCode getAddress(locaInfo,new JSONObject(webContent)); } else if(urlStr.indexOf(weatherUrl)!=-1){//4 得到天氣情況 webContent = sb.toString(); parserXml(webContent); } else{//2 得到本地資訊 locaInfo =webContent; getWebIp(cityCode); } } catch (Exception e) { weatherListner.error(e); e.printStackTrace(); } }; }.start(); }public void parserXml(String str){String wendu = str.substring(str.indexOf("<wendu>"), str.indexOf("</wendu>")).replace("<wendu>", "");String shidu = str.substring(str.indexOf("<shidu>"), str.indexOf("</shidu>")).replace("<shidu>", "");String pm25 = str.substring(str.indexOf("<pm25>"), str.indexOf("</pm25>")).replace("<pm25>", "");System.out.println("wendu:"+wendu+" shidu:"+shidu+" pm25:"+pm25); weatherListner.weatherInfo(city,wendu, shidu, pm25);} public void getAddress(String str,JSONObject cityJson){ try {JSONObject json = new JSONObject(str);String country = json.getString("country");String province = json.getString("province");city = json.getString("city");JSONArray ja_province = cityJson.getJSONArray("城市代碼");for(int i=0;i<ja_province.length();i++){JSONObject jo_province = ja_province.getJSONObject(i);if(jo_province.getString("省").equals(province)){JSONArray ja_city = jo_province.getJSONArray("市");for(int j=0;j<ja_city.length();j++){JSONObject jo_city = ja_city.getJSONObject(j);if(jo_city.getString("市名").equals(city)){String code = jo_city.getString("編碼");getWebIp(weatherUrl+code);System.out.println("code:"+code);break;}}break;}}} catch (JSONException e) {weatherListner.error(e);e.printStackTrace();} } public void getIp(String str){ try {JSONObject json = new JSONObject(str);String ip = json.getString("query");getWebIp(getAddress+ip);} catch (JSONException e) {weatherListner.error(e);e.printStackTrace();} } }
代碼調用該類 new WeatherCondition(this).getWebIp(WeatherCondition.getIpUrl);
並監聽WeatherCondition.SearchWeatherListner改介面
android 得到本地天氣情況