Ticker

6/recent/ticker-posts

Get weather info from Yahoo! Weather RSS Feed

The Yahoo! Weather RSS feed enables you to get up-to-date weather information, http://developer.yahoo.com/weather/.

The Weather RSS feed request follows simple HTTP GET syntax: start with a base URL and then add parameters and values after a question mark (?).

ex: Weather RSS feed for New York
http://weather.yahooapis.com/forecastrss?w=2459115

where 2459115 is WOEID for New York. You can find the code for any location from http://weather.yahoo.com/.

The another exercise show how to Search WOEID from http://query.yahooapis.com/.

The returned XML will like it:





Yahoo! Weather - New York, NY
http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/*http://weather.yahoo.com/forecast/USNY0996_f.html
Yahoo! Weather for New York, NY
en-us
Fri, 23 Mar 2012 8:49 pm EDT
60






Yahoo! Weather
142
18
http://weather.yahoo.com
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif


Conditions for New York, NY at 8:49 pm EDT
40.71
-74.01
http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/*http://weather.yahoo.com/forecast/USNY0996_f.html
Fri, 23 Mar 2012 8:49 pm EDT



Current Conditions:

Fair, 60 F


Forecast:

Fri - Mostly Cloudy. High: 72 Low: 52

Sat - PM Showers. High: 61 Low: 49



Full Forecast at Yahoo! Weather


(provided by The Weather Channel)

]]>



USNY0996_2012_03_24_7_00_EDT





Yahoo! Weather RSS Feed

It's a example to get Get weather info from Yahoo! Weather RSS Feed. Query Yahoo using HttpGet, then parse the returned XML using DOM. Please note that I only parse the returned RSS partially for demonstration, NOT ALL.

Get weather info from Yahoo! Weather RSS Feed

package com.exercise.AndroidYahooWeatherDOM;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
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.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidYahooWeatherDOMActivity extends Activity {

TextView weather;

class MyWeather{
String description;
String city;
String region;
String country;

String windChill;
String windDirection;
String windSpeed;

String sunrise;
String sunset;

String conditiontext;
String conditiondate;

public String toString(){

return "\n- " + description + " -\n\n"
+ "city: " + city + "\n"
+ "region: " + region + "\n"
+ "country: " + country + "\n\n"

+ "Wind\n"
+ "chill: " + windChill + "\n"
+ "direction: " + windDirection + "\n"
+ "speed: " + windSpeed + "\n\n"

+ "Sunrise: " + sunrise + "\n"
+ "Sunset: " + sunset + "\n\n"

+ "Condition: " + conditiontext + "\n"
+ conditiondate +"\n";

}
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
weather = (TextView)findViewById(R.id.weather);


String weatherString = QueryYahooWeather();
Document weatherDoc = convertStringToDocument(weatherString);

MyWeather weatherResult = parseWeather(weatherDoc);
weather.setText(weatherResult.toString());
}

private MyWeather parseWeather(Document srcDoc){

MyWeather myWeather = new MyWeather();

//Yahoo! Weather for New York, NY
myWeather.description = srcDoc.getElementsByTagName("description")
.item(0)
.getTextContent();

//
Node locationNode = srcDoc.getElementsByTagName("yweather:location").item(0);
myWeather.city = locationNode.getAttributes()
.getNamedItem("city")
.getNodeValue()
.toString();
myWeather.region = locationNode.getAttributes()
.getNamedItem("region")
.getNodeValue()
.toString();
myWeather.country = locationNode.getAttributes()
.getNamedItem("country")
.getNodeValue()
.toString();

//
Node windNode = srcDoc.getElementsByTagName("yweather:wind").item(0);
myWeather.windChill = windNode.getAttributes()
.getNamedItem("chill")
.getNodeValue()
.toString();
myWeather.windDirection = windNode.getAttributes()
.getNamedItem("direction")
.getNodeValue()
.toString();
myWeather.windSpeed = windNode.getAttributes()
.getNamedItem("speed")
.getNodeValue()
.toString();

//
Node astronomyNode = srcDoc.getElementsByTagName("yweather:astronomy").item(0);
myWeather.sunrise = astronomyNode.getAttributes()
.getNamedItem("sunrise")
.getNodeValue()
.toString();
myWeather.sunset = astronomyNode.getAttributes()
.getNamedItem("sunset")
.getNodeValue()
.toString();

//
Node conditionNode = srcDoc.getElementsByTagName("yweather:condition").item(0);
myWeather.conditiontext = conditionNode.getAttributes()
.getNamedItem("text")
.getNodeValue()
.toString();
myWeather.conditiondate = conditionNode.getAttributes()
.getNamedItem("date")
.getNodeValue()
.toString();

return myWeather;
}

private Document convertStringToDocument(String src){
Document dest = null;

DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder parser;

try {
parser = dbFactory.newDocumentBuilder();
dest = parser.parse(new ByteArrayInputStream(src.getBytes()));
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
Toast.makeText(AndroidYahooWeatherDOMActivity.this,
e1.toString(), Toast.LENGTH_LONG).show();
} catch (SAXException e) {
e.printStackTrace();
Toast.makeText(AndroidYahooWeatherDOMActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(AndroidYahooWeatherDOMActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
}

return dest;
}

private String QueryYahooWeather(){

String qResult = "";
String queryString = "http://weather.yahooapis.com/forecastrss?w=2459115";

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(queryString);

try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();

String stringReadLine = null;

while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}

qResult = stringBuilder.toString();
}

} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(AndroidYahooWeatherDOMActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(AndroidYahooWeatherDOMActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
}

return qResult;
}
}


main.xml

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/weather"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />




You have to modify AndroidManifest.xml to grand permission of "android.permission.INTERNET"

Download the files.

Next:
- Get description from Yahoo! Weather RSS feed, and display in WebView.
- Search WOEID from http://query.yahooapis.com/.
- Search WOEID and query Yahoo Weather
- Get Google Weather

Updated version:
- Get weather info from Yahoo! Weather RSS Feed in background thread

إرسال تعليق

0 تعليقات