今天项目新添加了得到本地当天的温度,湿度,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改接口