Saturday 15 September 2012

java - How to make Rss News Reader application in android ....? -



java - How to make Rss News Reader application in android ....? -

i have sample code parsing info xml , in xml parsing sax,doc,and xml pull parser best xml parser ,what json parser...? , how parse news feeds through json parser.please reply examples

public class mainactivity1 extends activity { private string finalurl="http://downloads.bbc.co.uk/podcasts/worldservice/globalnews/rss.xml"; private handlexml obj; private textview title,link,description; private linearlayout mweatherinfoslayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent = getintent(); finalurl = intent.getstringextra("url"); setcontentview(r.layout.activity_main_activity1); mweatherinfoslayout = (linearlayout) findviewbyid(r.id.weather_infos1); fetch(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } public void fetch(){ obj = new handlexml(finalurl); obj.fetchxml(); mweatherinfoslayout.removeallviews(); while(obj.parsingcomplete) { if(obj.gotit) { final linearlayout forecastinfolayout = (linearlayout) getlayoutinflater().inflate(r.layout.feedlist, null); final textview tvweather1 = (textview) forecastinfolayout.findviewbyid(r.id.textview_forecast_info1); final textview tvweather2 = (textview) forecastinfolayout.findviewbyid(r.id.textview_forecast_info2); final textview tvweather3 = (textview) forecastinfolayout.findviewbyid(r.id.textview_forecast_info3); final textview tvweather4 = (textview) forecastinfolayout.findviewbyid(r.id.textview_forecast_info4); tvweather1.settext(obj.gettitle()); tvweather4.settext(obj.getpubdate()); tvweather2.settext(obj.getlink()); string string = obj.getdescription(); string[] parts = string.split("<"); string part1 = parts[0]; tvweather3.settext(part1); mweatherinfoslayout.addview(forecastinfolayout); obj.setgotit(); } } } }

change handlexml class accordingly.

public class handlexml { private string title = "title"; private string link = "link"; private string description = "description"; private string urlstring = null; private xmlpullparserfactory xmlfactoryobject; public volatile boolean parsingcomplete = true; public handlexml(string url){ this.urlstring = url; } public string gettitle(){ homecoming title; } public string getlink(){ homecoming link; } public string getdescription(){ homecoming description; } public void parsexmlandstoreit(xmlpullparser myparser) { int event; string text=null; seek { event = myparser.geteventtype(); while (event != xmlpullparser.end_document) { string name=myparser.getname(); switch (event){ case xmlpullparser.start_tag: break; case xmlpullparser.text: text = myparser.gettext(); break; case xmlpullparser.end_tag: if(name.equals("title")){ title = text; } else if(name.equals("link")){ link = text; } else if(name.equals("description")){ description = text; } else{ } break; } event = myparser.next(); } parsingcomplete = false; } grab (exception e) { e.printstacktrace(); } } public void fetchxml(){ thread thread = new thread(new runnable(){ @override public void run() { seek { url url = new url(urlstring); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(10000 /* milliseconds */); conn.setconnecttimeout(15000 /* milliseconds */); conn.setrequestmethod("get"); conn.setdoinput(true); // starts query conn.connect(); inputstream stream = conn.getinputstream(); xmlfactoryobject = xmlpullparserfactory.newinstance(); xmlpullparser myparser = xmlfactoryobject.newpullparser(); myparser.setfeature(xmlpullparser.feature_process_namespaces, false); myparser.setinput(stream, null); parsexmlandstoreit(myparser); stream.close(); } grab (exception e) { } } }); thread.start(); } }

java android xml json rss

No comments:

Post a Comment