Android weather forecast - Yahoo Weather API - FULL SOURCE

Tutorials with advanced 'difficulty' and more Lines of Code.

Android weather forecast - Yahoo Weather API - FULL SOURCE

Postby nguyendat » Tue May 10, 2011 5:44 pm

OUTLINE
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.

:idea: Designed/Tested with sdk-version: Emulator Android SDK 2.2

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:
mainscreen.png
mainscreen.png (57.87 KiB) Viewed 7238 times


And location search screen:
searchlocation.png
searchlocation.png (37.13 KiB) Viewed 7238 times



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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         /***************************************************************************
  3.          * Get Document XML
  4.          * @param strURL URL string
  5.          * @return
  6.          * @throws IOException
  7.          * @date May 9, 2011
  8.          * @time 1:40:54 AM
  9.          * @author DatNQ
  10.          **************************************************************************/
  11.         public Document getDocumentFromURL(String strURL) throws IOException {
  12.                 /* Verify URL */
  13.                 if (strURL == null){
  14.                         Log.e(TAG,"Invalid input URL");
  15.                         return null;
  16.                 }
  17.                
  18.                 /* Connect to server */
  19.                 requestConnectServer(strURL);
  20.  
  21.                 /* Get data from server */
  22.                 String strDocContent = getDataFromConnection();
  23.                
  24.                 /* Close connection */
  25.                 requestDisconnect();
  26.                
  27.                 if (strDocContent == null){
  28.                         Log.e(TAG,"Can not get xml content");
  29.                         return null;
  30.                 }
  31.                
  32.                 int strContentSize = strDocContent.length();
  33.                 StringBuffer strBuff = new StringBuffer();
  34.                 strBuff.setLength(strContentSize+1);
  35.                 strBuff.append(strDocContent);
  36.                 ByteArrayInputStream is = new ByteArrayInputStream(strDocContent.getBytes());
  37.  
  38.                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  39.                 DocumentBuilder db;
  40.                 Document docData = null;
  41.                
  42.                 try {
  43.                         db = dbf.newDocumentBuilder();
  44.                         docData = db.parse(is);
  45.                 } catch (Exception e) {
  46.                         Log.e(TAG,"Parser data error");
  47.                         return null;
  48.                 }
  49.                
  50.                 return docData;
  51.         }
  52.        
  53.         /***************************************************************************
  54.          * Get xml document
  55.          * @return string xml
  56.          * @throws IOException
  57.          * @date May 9, 2011
  58.          * @time 1:43:28 AM
  59.          * @author DatNQ
  60.          **************************************************************************/
  61.         private String getDataFromConnection() throws IOException {
  62.                 if (httpConnection == null){
  63.                         Log.e(TAG,"connection is null");
  64.                         return null;
  65.                 }
  66.                
  67.                 String strValue = null;
  68.                 InputStream inputStream = httpConnection.getInputStream();
  69.                 if (inputStream == null){
  70.                         Log.e(TAG, "Get input tream error");
  71.                         return null;
  72.                 }
  73.                
  74.                 StringBuffer strBuf = new StringBuffer();              
  75.                 BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream));
  76.                 String strLine = "";
  77.                
  78.                 while ((strLine = buffReader.readLine()) != null){
  79.                         strBuf.append(strLine+"\n");
  80.                         strValue +=strLine+"\n";
  81.                 }
  82.                
  83.                 /* Release resource to system */
  84.                 buffReader.close();
  85.                 inputStream.close();
  86.                
  87.                 return strBuf.toString();
  88.         }
  89. }
  90. /*******************************************************************************
  91.  * END OF FILE
  92.  ******************************************************************************/
Parsed in 0.039 seconds, using GeSHi 1.0.8.4



Parser WEATHER:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public static WeatherInfo parserYahooWeatherInfo(Document docWeather){
  2.                 if (docWeather == null){
  3.                         Log.e(TAG,"Invalid doc weatehr");
  4.                         return null;
  5.                 }
  6.                
  7.                 String strCity = null;
  8.                 String strCountry = null;
  9.                 String strTempUnit = null;
  10.                 String strTempValue = null;
  11.                 String strHumidity = null;
  12.                 String strText = null;;
  13.                 String strCode = null;;
  14.                 String strDate = null;;
  15.                 String strVisi = null;
  16.                 try {
  17.                         Element root = docWeather.getDocumentElement();
  18.                         root.normalize();
  19.        
  20.                         NamedNodeMap locationNode = root.getElementsByTagName(PARAM_YAHOO_LOCATION).item(0).getAttributes();
  21.                        
  22.                         if (locationNode != null){
  23.                                 strCity = locationNode.getNamedItem(ATT_YAHOO_CITY).getNodeValue();
  24.                                 strCountry = locationNode.getNamedItem(ATT_YAHOO_COUNTRY).getNodeValue();
  25.                         }
  26.        
  27.                         NamedNodeMap unitNode = root.getElementsByTagName(PARAM_YAHOO_UNIT).item(0).getAttributes();
  28.                         if (unitNode != null){
  29.                                 strTempUnit = unitNode.getNamedItem(ATT_YAHOO_TEMP_UNIT).getNodeValue();
  30.                         }
  31.                        
  32.                         NamedNodeMap atmosNode = root.getElementsByTagName(PARAM_YAHOO_ATMOSPHERE).item(0).getAttributes();
  33.                         if (atmosNode != null){
  34.                                 strHumidity = atmosNode.getNamedItem(ATT_YAHOO_HUMIDITY).getNodeValue();
  35.                                 strVisi = atmosNode.getNamedItem(ATT_YAHOO_VISI).getNodeValue();
  36.                         }
  37.                        
  38.                         NamedNodeMap conditionNode = root.getElementsByTagName(PARAM_YAHOO_CONDITION).item(0).getAttributes();
  39.                         if (conditionNode != null){
  40.                                 strText = conditionNode.getNamedItem(ATT_YAHOO_TEXT).getNodeValue();
  41.                                 strCode = conditionNode.getNamedItem(ATT_YAHOO_CODE).getNodeValue();
  42.                                 strDate = conditionNode.getNamedItem(ATT_YAHOO_DATE).getNodeValue();
  43.                         }
  44.                        
  45.                         NamedNodeMap temNode = root.getElementsByTagName(PARAM_YAHOO_VALUE).item(0).getAttributes();
  46.                         if (temNode != null){
  47.                                 strTempValue = temNode.getNamedItem(ATT_YAHOO_TEMP).getNodeValue();
  48.                         }
  49.                 } catch (Exception e){
  50.                         Log.e(TAG,"Something wroing with parser data");
  51.                         return null;
  52.                 }
  53.  
  54.                 /* Weather info */
  55.                 WeatherInfo yahooWeatherInfo = new WeatherInfo(strCity, strCountry, strTempValue,
  56.                                 strHumidity, strText, strCode, strDate, strTempUnit, strVisi);
  57.                
  58.                 return yahooWeatherInfo;
  59.         }
  60.  
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
nguyendat
Freshman
Freshman
 
Posts: 3
Joined: Mon Jan 05, 2009 4:18 am

Top

Re: Android weather forecast - Yahoo Weather API - FULL SOUR

Postby suraj209 » Wed Feb 29, 2012 7:45 am

Hello sir,
I am a beginner in android. I cant understand how to invoke this api in my app. I installed apk on my emulator but i cant open it as a part of my app. plz suggest me......
Thank you
suraj209
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 29, 2012 6:50 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests