Привет!
Давайте я сегодня вам покажу как легко и просто можно достать данные о погоде для своего приложения на Java.
Я остановился на worldweatheronline.com API. Он отдает данные в XML, причем эти данные даже в бесплатной версии довольно подробны.
При регистрации аккаунта разработчика нам предоставляется строка с ключом, который позволяет нас идентифицировать. Дальше всё довольно просто:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
String line = null; String retLine = null; String point = null; try{ String addr= new String("http://api.worldweatheronline.com/free/v2/weather.ashx?"); if (WeatherClass.queryCity.equals(new String (""))) point = new String ("Irkutsk"); //поставим город для запроса String key = new String("f862a1983b282gg12932b181b3b38"); //тот самый ключик String query = new String(addr+"key="+key+"&q="+point); //сформируем строку запроса URL url = new URL(query); //зададим полный урл HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.xml"))); //считаем прилетевшие данные в xml while ((line = br.readLine())!= null){ retLine = line; bw.write((String) line); } System.out.println(line); br.close(); bw.close(); }catch(MalformedURLException me){ }catch(IOException ioe){ System.err.println("Error creating connection! "); ioe.printStackTrace(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ParseHandler extends DefaultHandler{ public String thisElement=""; @Override public void startDocument(){ System.out.println("STARTING PARSING XML"); } @Override public void endDocument(){ System.out.println("STOPPING PARSING XML"); } @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException{ thisElement = qName; } @Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException { thisElement = ""; } @Override public void characters(char[] ch, int start, int length) throws SAXException{ String smString = new String(ch, start, length); if(thisElement.equalsIgnoreCase("query")){ System.out.println("query is " + new String(ch, start, length)); WeatherClass.queryCity=smString; } if(thisElement.equalsIgnoreCase("maxtempc")){ System.out.println("maxtempc is " + smString); WeatherClass.maxTempC.add(Integer.valueOf(smString)); } if(thisElement.equalsIgnoreCase("mintempc")){ System.out.println("mintempc is " + smString); WeatherClass.minTempC.add(Integer.valueOf(smString)); } if(thisElement.equalsIgnoreCase("feelslikec")){ System.out.println("feelslikec is " + smString); WeatherClass.feelsLikeC.add(Integer.valueOf(smString)); } if(thisElement.equalsIgnoreCase("date")){ System.out.println("date is " + smString); WeatherClass.date.add(smString); } if(thisElement.equalsIgnoreCase("sunrise")){ System.out.println("sunrise: " + smString); WeatherClass.sunrise.add(smString); } if(thisElement.equalsIgnoreCase("sunset")){ System.out.println("sunset: " + smString); WeatherClass.sunset.add(smString); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
final public class WeatherClass { static boolean connection = true; static boolean updated; static String fullCity; static String queryCity = new String (""); static ArrayList<String> date = new ArrayList<String>(); static ArrayList<Integer> maxTempC = new ArrayList<Integer>(); static ArrayList<Integer> minTempC = new ArrayList<Integer>(); static ArrayList<Integer> feelsLikeC = new ArrayList<Integer>(); static ArrayList<String> sunrise = new ArrayList<String>(); static ArrayList<String> sunset = new ArrayList<String>(); public static void clearAll(){ date.clear(); maxTempC.clear(); minTempC.clear(); feelsLikeC.clear(); sunrise.clear(); sunset.clear(); } } |