i've tried retrieve rss data yahho weather ,however code couldn't work. may know what's wrong it? url i've use http://weather.yahooapis.com/forecastrss?w=1062617&u=c. i'm newbie in programming, kindly seek advice it.
thanks
the below coding.
cxactivity.java
public class cxactivity extends activity { /** called when activity first created. */ listview listview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); restclient rc = new restclient(); //the account key might not work. please insert own set. arraylist<weather> list1 = rc.getplaces("", ""); int index = 3; string[] listarray = new string[list1.size()]; stringbuilder sb = new stringbuilder(); int counter = 0; for(int = 1; i<list1.size(); i++) { sb.append("\n\nentry #: " + i); sb.append("\n" + "title: \""+ list1.get(i).gettitle() + "\""); sb.append("\n" + "publish date: \""+ list1.get(i).getpubdate() +"\""); sb.append("\n" + "condition: \""+ list1.get(i).getcondition()+"\""); sb.append("\n" + "forecast: \""+ list1.get(i).getforecast()+"\""); listarray[counter] = sb.tostring(); counter++; sb.delete(0, sb.capacity()); } listview=(listview)findviewbyid(r.id.listview01); listview.setadapter(new arrayadapter<string>(this,android.r.layout.simple_list_item_1 , listarray)); } }
restclient.java
public class restclient { arraylist weatherlist; //this function responsible pull data web service. public arraylist<weather> getplaces(string accountkey, string uniqueid) { weatherlist = new arraylist<weather>(); try { url _url = new url("http://weather.yahooapis.com/forecastrss?w=1062617&u=c"); urlconnection _urlconn = _url.openconnection(); _urlconn.addrequestproperty("accountkey", accountkey); _urlconn.addrequestproperty("uniqueuserid", uniqueid); bufferedreader br = new bufferedreader(new inputstreamreader(_urlconn.getinputstream())); string line = null; stringbuilder strbuilder = new stringbuilder(); while ((line = br.readline()) != null) { strbuilder.append(line); system.out.println(line); } string[] iproperties = strbuilder.tostring().split("<m:properties>"); (string str : iproperties) { weather weather = new weather(); weather.settitle(utils.getstringbetween(str, "<title>", "</<title>>")); weather.setpubdate(utils.getstringbetween(str, "<pubdate>", "</pubdate>")); weather.setcondition(utils.getstringbetween(str, "<yweather:condition>", "</yweather:condition>")); weather.setforecast(utils.getstringbetween(str, "<yweather:forecast>", "</yweather:forecast>")); weatherlist.add(weather); } } catch (malformedurlexception ex) { ex.printstacktrace(); } catch (ioexception ex) { ex.printstacktrace(); } catch (exception ex) { ex.printstacktrace(); } return weatherlist; } //this helper function specific traffic data structure public weather getplacesatindex(arraylist<weather> list, int index) { if(list.size() <= index) return null; return list.get(index); } }
utils.java
public class utils { //this functions xml data between xml elements public static string getstringbetween(string src, string start, string end) { stringbuilder sb = new stringbuilder(); int startidx = src.indexof(start) + start.length(); int endidx = src.indexof(end); while(startidx < endidx) { sb.append("" + string.valueof(src.charat(startidx))); startidx++; } return sb.tostring(); } }
weather.java
public class weather { string title; string pubdate; string condition; string forecast; public string gettitle() { return title; } public void settitle(string title) { title = title; } public string getpubdate() { return pubdate; } public void setpubdate(string pubdate) { pubdate = pubdate; } public string getcondition() { return condition; } public void setcondition(string condition) { condition = condition; } public string getforecast() { return forecast; } public void setforecast(string forecast) { forecast = forecast; } }
please change activity code.
public class mainactivity extends activity
{ /** called when activity first created. */
listview listview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); restclient rc = new restclient(); // account key might not work. please insert own set. arraylist<weather> list1 = rc.getplaces("", ""); int index = 3; string[] listarray = new string[list1.size()]; stringbuilder sb = new stringbuilder(); int counter = 0; log.e("n", "listarray::" + list1.size()); (int = 0; < list1.size(); i++) { sb.append("\n\nentry #: " + i); sb.append("\n" + "title: \"" + list1.get(i).gettitle() + "\""); sb.append("\n" + "publish date: \"" + list1.get(i).getpubdate() + "\""); sb.append("\n" + "condition: \"" + list1.get(i).getcondition() + "\""); sb.append("\n" + "forecast: \"" + list1.get(i).getforecast() + "\""); listarray[counter] = sb.tostring(); counter++; sb.delete(0, sb.capacity()); } log.e("n", "listarray::" + listarray.length); listview = (listview) findviewbyid(r.id.listview); listview.setadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1, listarray)); }
}
Comments
Post a Comment