hello i'm trying parse rss feed website , display on android application , problem weird data. mean multiple lines of same of rss.
the rss feed
rss version="2.0"> <channel> <title>recent seismicity in greece</title> <link> http://www.geophysics.geol.uoa.gr/stations/maps/recent.html </link> <description>real-time seismicity</description> <pubdate>wed, 3 june 2015 06:44:01 gmt</pubdate> <lastbuilddate>wed, 3 june 2015 06:44:01 gmt</lastbuilddate> <webmaster>vkapetan@geol.uoa.gr (vasilis kapetanidis)</webmaster> <item> <title>m 2.7, 03/06 - 05:43:06 , 65.7 km sw of rethimnon</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktzh&lng=en ]]> </link> <description> 65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7 </description> <pubdate>wed, 3 june 2015 05:44:52</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktzh&lng=en ]]> </guid> </item> <item> <title>m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktvz&lng=en ]]> </link> <description> 134.4 km sse of iraklion<br> time: 03-jun-2015 04:02:39 (utc) <br> latitude: 34.17n <br> longitude: 25.58e <br> depth: 10km <br> m 2.7 </description> <pubdate>wed, 3 june 2015 04:05:33</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktvz&lng=en ]]> </guid> </item> <item> <title>m 1.2, 03/06 - 03:34:54 , 22.2 km n of aegion</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktvb&lng=en ]]> </link> <description> 22.2 km n of aegion<br> time: 03-jun-2015 03:34:54 (utc) <br> latitude: 38.45n <br> longitude: 22.06e <br> depth: 14km <br> m 1.2 </description> <pubdate>wed, 3 june 2015 03:36:03</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktvb&lng=en ]]> </guid> </item> <item> <title>m 2.2, 03/06 - 03:30:08 , 64.3 km ssw of rethimnon</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktux&lng=en ]]> </link> <description> 64.3 km ssw of rethimnon<br> time: 03-jun-2015 03:30:08 (utc) <br> latitude: 34.88n <br> longitude: 24.09e <br> depth: 22km <br> m 2.2 </description> <pubdate>wed, 3 june 2015 03:32:37</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktux&lng=en ]]> </guid> </item> <item> <title>m 1.6, 03/06 - 03:11:00 , 35.5 km wnw of preveza</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktug&lng=en ]]> </link> <description> 35.5 km wnw of preveza<br> time: 03-jun-2015 03:11:00 (utc) <br> latitude: 39.10n <br> longitude: 20.39e <br> depth: 10km <br> m 1.6 </description> <pubdate>wed, 3 june 2015 03:12:18</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktug&lng=en ]]> </guid> </item> <item> <title>m 1.2, 03/06 - 02:06:55 , 28.6 km wnw of amfissa</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktsd&lng=en ]]> </link> <description> 28.6 km wnw of amfissa<br> time: 03-jun-2015 02:06:55 (utc) <br> latitude: 38.63n <br> longitude: 22.07e <br> depth: 10km <br> m 1.2 </description> <pubdate>wed, 3 june 2015 02:08:12</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktsd&lng=en ]]> </guid> </item> <item> <title>m 1.9, 03/06 - 02:00:30 , 33.7 km nnw of kalamata</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktry&lng=en ]]> </link> <description> 33.7 km nnw of kalamata<br> time: 03-jun-2015 02:00:30 (utc) <br> latitude: 37.32n <br> longitude: 21.96e <br> depth: 10km <br> m 1.9 </description> <pubdate>wed, 3 june 2015 02:03:30</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktry&lng=en ]]> </guid> </item> <item> <title>m 1.6, 03/06 - 01:46:45 , 6.5 km wsw of argostoli</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktrm&lng=en ]]> </link> <description> 6.5 km wsw of argostoli<br> time: 03-jun-2015 01:46:45 (utc) <br> latitude: 38.15n <br> longitude: 20.41e <br> depth: 5km <br> m 1.6 </description> <pubdate>wed, 3 june 2015 01:48:01</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktrm&lng=en ]]> </guid> </item> <item> <title>m 1.3, 03/06 - 01:16:17 , 25.2 km ssw of levadhia</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktqm&lng=en ]]> </link> <description> 25.2 km ssw of levadhia<br> time: 03-jun-2015 01:16:17 (utc) <br> latitude: 38.23n <br> longitude: 22.75e <br> depth: 5km <br> m 1.3 </description> <pubdate>wed, 3 june 2015 01:17:24</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktqm&lng=en ]]> </guid> </item> <item> <title>m 2.6, 03/06 - 01:04:31 , 65.8 km ssw of karpathos</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktqc&lng=en ]]> </link> <description> 65.8 km ssw of karpathos<br> time: 03-jun-2015 01:04:31 (utc) <br> latitude: 35.14n <br> longitude: 26.81e <br> depth: 10km <br> m 2.6 </description> <pubdate>wed, 3 june 2015 01:06:20</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktqc&lng=en ]]> </guid> </item> <item> <title> m 1.8, 03/06 - 00:54:25 , 31.9 km se of karpenision </title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktpt&lng=en ]]> </link> <description> 31.9 km se of karpenision<br> time: 03-jun-2015 00:54:25 (utc) <br> latitude: 38.62n <br> longitude: 22.02e <br> depth: 5km <br> m 1.8 </description> <pubdate>wed, 3 june 2015 00:59:03</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktpt&lng=en ]]> </guid> </item> <item> <title>m 1.4, 03/06 - 00:49:29 , 28.4 km nw of amfissa</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktpp&lng=en ]]> </link> <description> 28.4 km nw of amfissa<br> time: 03-jun-2015 00:49:29 (utc) <br> latitude: 38.67n <br> longitude: 22.11e <br> depth: 10km <br> m 1.4 </description> <pubdate>wed, 3 june 2015 00:53:05</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktpp&lng=en ]]> </guid> </item> <item> <title>m 3.4, 03/06 - 00:42:31 , 76.0 km ssw of karpathos</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktpj&lng=en ]]> </link> <description> 76.0 km ssw of karpathos<br> time: 03-jun-2015 00:42:31 (utc) <br> latitude: 35.01n <br> longitude: 26.91e <br> depth: 10km <br> m 3.4 </description> <pubdate>wed, 3 june 2015 01:19:18</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktpj&lng=en ]]> </guid> </item> <item> <title>m 1.9, 03/06 - 00:39:40 , 37.3 km wsw of kithira</title> <link> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktph&lng=en ]]> </link> <description> 37.3 km wsw of kithira<br> time: 03-jun-2015 00:39:40 (utc) <br> latitude: 36.08n <br> longitude: 22.66e <br> depth: 10km <br> m 1.9 </description> <pubdate>wed, 3 june 2015 00:42:40</pubdate> <guid> <![cdata[ http://www.geophysics.geol.uoa.gr/stations/gmaps3/eventpage.php?scid=uoa2015ktph&lng=en ]]> </guid> </item>
my parser
package com.sarakinosproductions.bampis.gquakes; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlpullparserfactory; import android.util.log; import java.io.inputstream; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; /** * created bampis on 2/6/2015. */ public class quakeparser { public final string parser_tag = "parser_msg"; private arraylist<quakedata> qdata; private string urlstring = null; private xmlpullparserfactory xmlfactoryobject; public volatile boolean parsingcomplete = true; //constructor gia na dosoume url pou exei rss public quakeparser(string seismoiurl){ this.urlstring = seismoiurl; qdata = new arraylist<>(); } public void parsexmlandstoreit(xmlpullparser myparser) { int event; string text=null; //temp strings string title = ""; string description = ""; string pubdate = ""; try { 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("description")){ description =text; } else if(name.equals("pubdate")){ pubdate = text; } else{ } break; } //logging data log.i("rss row", "title:" + title + "//" + "description:" + description + "//" + "published date:" + pubdate); //storing data qdata.add(new quakedata(title,description,pubdate)); event = myparser.next(); } parsingcomplete = false; } catch (exception e) { e.printstacktrace(); } } public arraylist<quakedata> fetchxml() throws interruptedexception { thread thread = new thread(new runnable() { @override public void run() { try { url url = new url(urlstring); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(10000 /* milliseconds */); conn.setconnecttimeout(15000 /* milliseconds */); conn.setrequestmethod("get"); conn.setdoinput(true); 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(); } catch (exception e) { e.printstacktrace(); } } }); thread.start(); thread.join(); return qdata; } }
a sample of log
6-03 09:43:24.162 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 05:43:06 , 65.7 km sw of rethimnon//description:real-time seismicity//published date:wed, 3 june 2015 06:43:01 gmt 06-03 09:43:24.162 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 05:43:06 , 65.7 km sw of rethimnon//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.162 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 05:43:06 , 65.7 km sw of rethimnon//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.162 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:65.7 km sw of rethimnon<br> time: 03-jun-2015 05:43:06 (utc) <br> latitude: 34.90n <br> longitude: 24.02e <br> depth: 11km <br> m 2.7//published date:wed, 3 june 2015 05:44:52 06-03 09:43:24.163 25029-25065/? i/rss row﹕ title:m 2.7, 03/06 - 04:02:39 , 134.4 km sse of iraklion//description:134.4 km sse of iraklion<br> time: 03-jun-2015 04:02:39 (utc) <br> latitude: 34.17n <br> longitude: 25.58e <br> depth: 10km <br> m 2.7//published date:wed, 3 june 2015 05:44:52
i've make progress , i've found solution. problem haven't specified flag understanding when parsing progress in items i've cared for. new working approach time using asynctask .
here new parser
public class dataparser extends asynctask { public final string parser_log_tag = "parser_msg"; private string xmlurl; private url url; arraylist<quake> qdata; arraylist<string> title; arraylist<string> description; arraylist<string> pubdate; public dataparser(string xmlurl1) { qdata = new arraylist<>(); this.xmlurl = xmlurl1; this.title = new arraylist<>(); this.pubdate = new arraylist<>(); this.description = new arraylist<>(); } @override protected object doinbackground(object[] params) { try { //boolean gia na vlepoume eimaste mesa se item kai oxi sto channel boolean insideitem = false; //dinoume url pou exei rss feed url = new url(xmlurl); //dimiourgoume kai arxikopoioume ton xmlpullparser mas xmlpullparserfactory parserfactory = xmlpullparserfactory.newinstance(); parserfactory.setnamespaceaware(false); xmlpullparser parser = parserfactory.newpullparser(); //orizoume input stream gia ton parser mas parser.setinput(getinputstream(url), "utf_8"); int eventtype = parser.geteventtype(); while (eventtype != xmlpullparser.end_document) { if (eventtype == xmlpullparser.start_tag) { if (parser.getname().equalsignorecase("item")) { insideitem = true; } else if (parser.getname().equalsignorecase("title")) { if (insideitem) title.add(parser.nexttext()); //extract title } else if (parser.getname().equalsignorecase("description")) { if (insideitem) description.add(parser.nexttext()); //extract description } else if (parser.getname().equalsignorecase("pubdate")) { if (insideitem) pubdate.add(parser.nexttext()); //extract description } } else if (eventtype == xmlpullparser.end_tag && parser.getname().equalsignorecase("item")) { insideitem = false; } eventtype = parser.next(); //move next element } } catch (malformedurlexception e) { e.printstacktrace(); } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } for(int i=0;i<title.size();i++){ qdata.add(new quake(title.get(i),description.get(i),pubdate.get(i))); } return qdata; } private inputstream getinputstream(url url) { inputstream inputstream = null; try { urlconnection connection = url.openconnection(); connection.setconnecttimeout(15000); connection.setreadtimeout(10000); connection.setdoinput(true); connection.connect(); inputstream = connection.getinputstream(); return inputstream; } catch (ioexception ioe) { ioe.printstacktrace(); } return inputstream; } public arraylist<quake> data() { return qdata; } }
Comments
Post a Comment