example:

Current Conditions:
Cloudy, 47 F
Forecast:
Sun - Few Showers. High: 57 Low: 47
Mon - Sunny/Wind. High: 53 Low: 30
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]>
This example demonstrate how to retrieve description elements from Yahoo! Weather RSS feed, and display on a WebView.

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.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
public class AndroidYahooWeatherDOMActivity extends Activity {
WebView webView;
/** 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);
webView = (WebView)findViewById(R.id.webview);
String weatherString = QueryYahooWeather();
Document weatherDoc = convertStringToDocument(weatherString);
String weatherResult = parseWeatherDescription(weatherDoc);
webView.loadData(weatherResult, "text/html", "UTF-8");
}
private String parseWeatherDescription(Document srcDoc){
String weatherDescription="";
NodeList nodeListDescription = srcDoc.getElementsByTagName("description");
if(nodeListDescription.getLength()>=0){
for(int i=0; i weatherDescription += nodeListDescription.item(i).getTextContent()+"
";
}
}else{
weatherDescription = ("No Description!");
}
return weatherDescription;
}
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;
}
}
Modify main.xml to add a WebView.
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:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
You have to modify AndroidManifest.xml to grand permission of "android.permission.INTERNET"

Related:
- Search WOEID from http://query.yahooapis.com/.
- Get Google Weather
0 تعليقات