This is almost my very first tutorials, feel free to give your comment
For Google Weather API you could refer tutorial of plusminus at:
- Code: Select all
http://www.anddev.org/advanced-tutorials-f21/android-weather-forecast-google-weather-api-description-t337.html
NOTE
In this tutorial i use format like plusminus
What you learn: You will learn how to access the Yahoo Weather API an parse the XML-Result using a DOM parser.
What this contains:
* Access the Yahoo Weather API by WOEID
* Get WOEID and WEATHER by location (Big thanks to vicchi from http://stackoverflow.com/questions/1822 ... -retrieval)
* Parse XML Files (Streams) using the DOM
* Add widget version (04/07/2011)
SCREENSHOT
What it will look like:
And location search screen:
YAHOO API
In this tutorial i use 2 API from yahoo API, one fo archiving WOEID by location and other for get weather by WOEID:
1.http://query.yahooapis.com/v1/public/yq ... format=xml
2. http://weather.yahooapis.com/forecastrss?w=%s&u=c
BUSINESS FLOW
1. Input Location
2. Get WOEID (Where on Earth IDentifier) by location
3. Get weather by WOEID
CODE SNIPPET:
1. Connect to server get XML data:
Using java Syntax Highlighting
- /***************************************************************************
- * Get Document XML
- * @param strURL URL string
- * @return
- * @throws IOException
- * @date May 9, 2011
- * @time 1:40:54 AM
- * @author DatNQ
- **************************************************************************/
- public Document getDocumentFromURL(String strURL) throws IOException {
- /* Verify URL */
- if (strURL == null){
- Log.e(TAG,"Invalid input URL");
- return null;
- }
- /* Connect to server */
- requestConnectServer(strURL);
- /* Get data from server */
- String strDocContent = getDataFromConnection();
- /* Close connection */
- requestDisconnect();
- if (strDocContent == null){
- Log.e(TAG,"Can not get xml content");
- return null;
- }
- int strContentSize = strDocContent.length();
- StringBuffer strBuff = new StringBuffer();
- strBuff.setLength(strContentSize+1);
- strBuff.append(strDocContent);
- ByteArrayInputStream is = new ByteArrayInputStream(strDocContent.getBytes());
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db;
- Document docData = null;
- try {
- db = dbf.newDocumentBuilder();
- docData = db.parse(is);
- } catch (Exception e) {
- Log.e(TAG,"Parser data error");
- return null;
- }
- return docData;
- }
- /***************************************************************************
- * Get xml document
- * @return string xml
- * @throws IOException
- * @date May 9, 2011
- * @time 1:43:28 AM
- * @author DatNQ
- **************************************************************************/
- private String getDataFromConnection() throws IOException {
- if (httpConnection == null){
- Log.e(TAG,"connection is null");
- return null;
- }
- String strValue = null;
- InputStream inputStream = httpConnection.getInputStream();
- if (inputStream == null){
- Log.e(TAG, "Get input tream error");
- return null;
- }
- StringBuffer strBuf = new StringBuffer();
- BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream));
- String strLine = "";
- while ((strLine = buffReader.readLine()) != null){
- strBuf.append(strLine+"\n");
- strValue +=strLine+"\n";
- }
- /* Release resource to system */
- buffReader.close();
- inputStream.close();
- return strBuf.toString();
- }
- }
- /*******************************************************************************
- * END OF FILE
- ******************************************************************************/
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Parser WEATHER:
Using java Syntax Highlighting
- public static WeatherInfo parserYahooWeatherInfo(Document docWeather){
- if (docWeather == null){
- Log.e(TAG,"Invalid doc weatehr");
- return null;
- }
- String strCity = null;
- String strCountry = null;
- String strTempUnit = null;
- String strTempValue = null;
- String strHumidity = null;
- String strText = null;;
- String strCode = null;;
- String strDate = null;;
- String strVisi = null;
- try {
- Element root = docWeather.getDocumentElement();
- root.normalize();
- NamedNodeMap locationNode = root.getElementsByTagName(PARAM_YAHOO_LOCATION).item(0).getAttributes();
- if (locationNode != null){
- strCity = locationNode.getNamedItem(ATT_YAHOO_CITY).getNodeValue();
- strCountry = locationNode.getNamedItem(ATT_YAHOO_COUNTRY).getNodeValue();
- }
- NamedNodeMap unitNode = root.getElementsByTagName(PARAM_YAHOO_UNIT).item(0).getAttributes();
- if (unitNode != null){
- strTempUnit = unitNode.getNamedItem(ATT_YAHOO_TEMP_UNIT).getNodeValue();
- }
- NamedNodeMap atmosNode = root.getElementsByTagName(PARAM_YAHOO_ATMOSPHERE).item(0).getAttributes();
- if (atmosNode != null){
- strHumidity = atmosNode.getNamedItem(ATT_YAHOO_HUMIDITY).getNodeValue();
- strVisi = atmosNode.getNamedItem(ATT_YAHOO_VISI).getNodeValue();
- }
- NamedNodeMap conditionNode = root.getElementsByTagName(PARAM_YAHOO_CONDITION).item(0).getAttributes();
- if (conditionNode != null){
- strText = conditionNode.getNamedItem(ATT_YAHOO_TEXT).getNodeValue();
- strCode = conditionNode.getNamedItem(ATT_YAHOO_CODE).getNodeValue();
- strDate = conditionNode.getNamedItem(ATT_YAHOO_DATE).getNodeValue();
- }
- NamedNodeMap temNode = root.getElementsByTagName(PARAM_YAHOO_VALUE).item(0).getAttributes();
- if (temNode != null){
- strTempValue = temNode.getNamedItem(ATT_YAHOO_TEMP).getNodeValue();
- }
- } catch (Exception e){
- Log.e(TAG,"Something wroing with parser data");
- return null;
- }
- /* Weather info */
- WeatherInfo yahooWeatherInfo = new WeatherInfo(strCity, strCountry, strTempValue,
- strHumidity, strText, strCode, strDate, strTempUnit, strVisi);
- return yahooWeatherInfo;
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
REFERENCE
1. http://stackoverflow.com/questions/1822 ... -retrieval
2. http://developer.yahoo.com/weather/
APK
http://code.google.com/p/yahooweatheran ... k&can=2&q=
FULL SOURCE CODE
http://code.google.com/p/yahooweatherandroid/
SVN:
https://yahooweatherandroid.googlecode.com/svn/trunk
Regards,
DatNQ

