您好我试图从网站解析RSS源并将其显示在我的Android应用程序上,问题是我得到了奇怪的数据。我的意思是我得到了RSS的多行。
RSS提要
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>
我的解析器
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 by 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 to url pou exei to 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 the data
Log.i("RSS ROW", "Title:" + title + "//" + "Description:" + description + "//" + "Published Date:" + pubDate);
//Storing the 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;
}
}
我的日志样本
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
答案 0 :(得分:0)
我已经取得了进步,并且我找到了解决方案。问题是我没有指定一个标志,用于理解解析进度何时在我所关注的项目中。我的新工作方法是使用AsyncTask。
这是我的新解析器
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 an eimaste mesa se item kai oxi sto channel
boolean insideItem = false;
//Dinoume to url pou exei to rss feed
url = new URL(xmlUrl);
//Dimiourgoume kai arxikopoioume ton XmlPullParser mas
XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
parserFactory.setNamespaceAware(false);
XmlPullParser parser = parserFactory.newPullParser();
//Orizoume to 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 the title
} else if (parser.getName().equalsIgnoreCase("description")) {
if (insideItem)
description.add(parser.nextText()); //extract the description
} else if (parser.getName().equalsIgnoreCase("pubDate")) {
if (insideItem)
pubDate.add(parser.nextText()); //extract the description
}
} else if (eventType == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
eventType = parser.next(); //move to 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;
}
}