我一直在尝试在网上找到的代码,它允许解析xml文件 logcat中显示了不同的解析字段。
解析在doInBackground()中的AsyncTask中完成,从而创建了一个列表,用于显示特定URL的字段并在logcat中正确显示。
我现在要做的是检查我是否可以在执行后显示字段 所以我移动了注释掉的代码块,允许从doinBackground()到onPostExecute()的logcat显示。
当我编译我的代码时,我收到一个错误:方法没有覆盖或实现超类型的方法
我该如何解决这个问题?我缺少什么基本的东西?
public class MainActivity extends Activity {
public static final String LOG_TAG = "MainActivity.java";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// parse our XML
new parseXmlAsync().execute();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private class parseXmlAsync extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
try {
// initialize our input source variable
InputSource inputSource = null;
// XML from URL
URL url = new URL(
"http://feeds.news24.com/articles/fin24/Tech/rss");
inputSource = new InputSource(url.openStream());
// instantiate SAX parser
SAXParserFactory saxParserFactory = SAXParserFactory
.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
// get the XML reader
XMLReader xmlReader = saxParser.getXMLReader();
// prepare and set the XML content or data handler before
// parsing
XmlContentHandler xmlContentHandler = new XmlContentHandler();
xmlReader.setContentHandler(xmlContentHandler)
// parse the XML input source
xmlReader.parse(inputSource);
// put the parsed data to a List
List<ParsedDataSet> parsedDataSet = xmlContentHandler.getParsedData();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<ParsedDataSet> parsedDataSet) {
// use an iterator so we can loop through the data
Iterator<ParsedDataSet> i = parsedDataSet.iterator();
ParsedDataSet dataItem;
while (i.hasNext()) {
dataItem = (ParsedDataSet) i.next();
/*
* parentTag can also represent the main type of data
*/
String parentTag = dataItem.getParentTag();
Log.v(LOG_TAG, "parentTag: " + parentTag);
if (parentTag.equals("item")) {
Log.v(LOG_TAG, "Title: " + dataItem.getTitle());
Log.v(LOG_TAG, "Description: " + dataItem.getDescription());
Log.v(LOG_TAG, "URL: " + dataItem.getLink());
}
}
}
}
}
答案 0 :(得分:1)
parseXmlAsync extends AsyncTask<String, String, String>
这意味着您有<Params, Progress, Result>
<String, String, String>
。
1)你没有Progress
。那可以是Void
。
2)您似乎不关心Params
或String... strings
。所以这也可以是Void
。
3)如果您希望onPostExecute(List<ParsedDataSet> parsedDataSet)
有效,则需要将Result
设为List<ParsedDataSet>
。
从doInBackground
返回给onPostExecute
的值。您只返回null
,因此即使这确实有效,parsedDataSet.iterator();
也会抛出NullPointerException。
现在,要修复错误,需要您实现
onPostExecute(String result)
但是,您应该使用
ParseXmlAsync extends AsyncTask<Void, Void, List<ParsedDataSet>>
和
public List<ParsedDataSet> doInBackground(Void... params)
然后,来自return parsedDataSet
doInBackground
答案 1 :(得分:1)
您有一个错误,因为您的AsyncTask签名是AsyncTask<String, String, String>
而onPostExecute
方法的签名是onPostExecute(List<ParsedDataSet> parsedDataSet)
而不是onPostExecute(String response)
。
你有两种方法可以做到这一点,
方法1
在parsedDataSet
中将parseXmlAsync
设为全局。
private class parseXmlAsync extends AsyncTask<String, String, String> {
List<ParsedDataSet> parsedDataSet;
@Override
protected String doInBackground(String... strings) {
...
parsedDataSet = xmlContentHandler.getParsedData();
...
}
@Override
protected void onPostExecute(String response) {
Iterator<ParsedDataSet> i = parsedDataSet.iterator();
...
}
}
方法2
更改AsyncTask
private class parseXmlAsync extends AsyncTask<String, String, List<ParsedDataSet>> {
List<ParsedDataSet> parsedDataSet;
@Override
protected String doInBackground(String... strings) {
...
parsedDataSet = xmlContentHandler.getParsedData();
return parsedDataSet;
...
}
@Override
protected void onPostExecute(List<ParsedDataSet> parsedDataSet) {
Iterator<ParsedDataSet> i = parsedDataSet.iterator();
...
}
}
答案 2 :(得分:0)
问题是你在AysncTask中犯了错误。请参阅我的以下代码。
private class parseXmlAsync extends AsyncTask<String, String, List<ParsedDataSet>>
{
@Override
protected List<ParsedDataSet> doInBackground(String... strings)
{
try {
// initialize our input source variable
InputSource inputSource = null;
// XML from URL
URL url = new URL("http://feeds.news24.com/articles/fin24/Tech/rss");
inputSource = new InputSource(url.openStream());
// instantiate SAX parser
SAXParserFactory saxParserFactory = SAXParserFactory
.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
// get the XML reader
XMLReader xmlReader = saxParser.getXMLReader();
// prepare and set the XML content or data handler before
// parsing
XmlContentHandler xmlContentHandler = new XmlContentHandler();
xmlReader.setContentHandler(xmlContentHandler);
xmlReader.parse(inputSource);
List parsedDataSet = xmlContentHandler.getParsedData();
return parsedDataSet;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<ParsedDataSet> parsedDataSet) {
// use an iterator so we can loop through the data
Iterator<ParsedDataSet> i = parsedDataSet.iterator();
ParsedDataSet dataItem;
while (i.hasNext()) {
dataItem = (ParsedDataSet) i.next();
/*
* parentTag can also represent the main type of data
*/
String parentTag = dataItem.getParentTag();
Log.v(LOG_TAG, "parentTag: " + parentTag);
if (parentTag.equals("item")) {
Log.v(LOG_TAG, "Title: " + dataItem.getTitle());
Log.v(LOG_TAG, "Description: " + dataItem.getDescription());
Log.v(LOG_TAG, "URL: " + dataItem.getLink());
}
}
}
}