我正在尝试获取一个干净的 XML 文件,以便我可以解析数据。这是我的尝试:
private class cleanHtml extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
try {
HtmlCleaner cleaner = new HtmlCleaner();
String url = "https://www.easistent.com/urniki/263/razredi/16515";
TagNode node = cleaner.clean(new URL(url));
CleanerProperties props = cleaner.getProperties();
new PrettyXmlSerializer(props).writeToFile(node, "cleaned.xml", "utf-8");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
我必须使用 AsyncTask,因为如果我在主 UI 上执行此操作,应用程序就会崩溃。
现在我正在尝试执行此操作:
cleanHtml.execute();
但是,这不起作用。它强调了“cleanHtml.execute();”并这样说:
Cannot make a static reference to the non-static method execute(Void...) from the type AsyncTask
知道我现在如何运行这个 AsyncTask 吗?我做得对吗?另外,一旦我开始工作,我在哪里可以看到输出的 clean.xml 文件?因为我没有设置任何“输出文件夹”。
最佳答案
使用
new cleanHtml.execute();
而不是
cleanHtml.execute();
用于执行AsyncTask
,因为execute()不是AsyncTask
类的静态方法,因此您需要创建类的实例来调用execute()
方法
关于java - 使用 HtmlCleaner 和 AsyncTask 清理 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19009951/