RSS simply means Really Simple syndication, this is a xml document which is created by a website.
You need to parse this xml file and show the information:-
Here is the code that can help you :-
Step 1:-Declare the variable of which you want to extract information:-
private String windSpeed, city,country,windChillDegree;
Step 2:-Function to Fetch String response using url:-
private String fetchResponse()
{
String result = "";
String myQuery = "http://weather.yahooapis.com/forecastrss?w=2459115";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(myQuery);
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");
}
result = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
Step 3:-Create a function that convert above string response to Document:-
private Document convertStringToDocument(String src)
{
Document document = null;
DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder parser;
try {
parser = dbFactory.newDocumentBuilder();
document = 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 document;
}
Step 4:-Create a function that parse the converted document:-
private void parseWeather(Document srcDoc)
{
Node locationNode = srcDoc.getElementsByTagName("yweather:location").item(0);
city = locationNode.getAttributes()
.getNamedItem("city")
.getNodeValue()
.toString();
country = locationNode.getAttributes()
.getNamedItem("country")
.getNodeValue()
.toString();
Node windNode = srcDoc.getElementsByTagName("yweather:wind").item(0);
windChill = windNode.getAttributes()
.getNamedItem("chill")
.getNodeValue()
.toString();
windChillDegree = windNode.getAttributes()
.getNamedItem("speed")
.getNodeValue()
.toString();
}
Step 5:- Now create the asynctask where you can call the function to fetch String response:-
class FetchXmlDocumentAsynctask extends AsyncTask<String, Void, String>
{
private Exception exception;
protected String doInBackground(String... urls)
{
String info= fetchYahooInformatiom();
return info;
}
protected void onPostExecute(String info) {
Document weatherDoc = convertStringToDocument(info);
parseWeather(weatherDoc);
/*
Here you got values in variables "windSpeed","city","country","windChillDegree",
now you can show them in textfields
*/
}
}
0 Comment(s)