Today, the new project added to get the local day temperature, humidity, PM2.5 value of the demand, research, write down the results of labor, for the code farmers to make less detours to contribute.
Ideas are as follows:
1. Get the phone's extranet IP (Http://ip-api.com/json)
2. Get local information: Province and City name (http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=)
3. Get the city code according to the province city name (Https://code.csdn.net/snippets/621277/master/blog_20150317_1_5052706/raw)
4. Weather conditions by City Code (http://wthrcdn.etouch.cn/WeatherApi?citykey=)
Of course, there's a lot of weather in here. Temperature, humidity and PM2.5
The specific code is as follows:
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 = "HT Tp://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 voi D error (Exception e); } public weathercondition (Searchweatherlistner Weatherlistner) {super (); this.weatherlistner = Weatherlistner;} public void Getwebip (final String urlstr) {new Thread () {public void Run () {string str Foreignip = ""; 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 Get IP getip (webcontent); }else if (urlstr.equals (Citycode)) {//3 get all Citycode getaddress (locainfo,new jsonobject (webcontent)); } else if (Urlstr.indexof (Weatherurl)!=-1) {//4 get weather conditions webcontent = Sb.tostring (); Parserxml (webcontent); } ELSE{//2 get local information 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 ("City Code"); for (int i=0;i<ja_province.length (); i++) {Jsonobject jo_ Province = Ja_province.getjsonobject (i), if (Jo_province.getstring ("province"). Equals (province){Jsonarray ja_city = Jo_province.getjsonarray ("city"); for (int j=0;j<ja_city.length (); j + +) {Jsonobject jo_city = Ja_ City.getjsonobject (j); if (jo_city.getstring (city)) {String code = jo_city.getstring ("encoding"); 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 ();} } }
The code calls the class new Weathercondition (This). GETWEBIP (Weathercondition.getipurl);
and monitor the Weathercondition.searchweatherlistner interface
Android gets local weather conditions