1

我正在尝试使用 AsyncTask 使用 HtmlCleaner 清理 HTML。这是代码:

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");
            Log.i("TAG", "Executing AsyncTask");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

我在 onCreate 方法中执行 AsyncTask:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new cleanHtml().execute();

}//End of onCreate

然而,什么也没有发生。如您所见,我将 log.i 放在 AsyncTask 中以查看它是否正在执行 r ,但我从未在 logcat 中看到 log.i 消息。我可能做错了什么?另外,“cleaned.xml”会出现在我手机的哪个位置?因为我没有设置任何类型的目标文件夹。

日志猫(信息):

09-25 20:23:31.739: W/System.err(19078): java.io.FileNotFoundException: /cleaned.xml: open failed: EROFS (Read-only file system)
09-25 20:23:31.747: W/System.err(19078):    at libcore.io.IoBridge.open(IoBridge.java:409)
09-25 20:23:31.747: W/System.err(19078):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-25 20:23:31.747: W/System.err(19078):    at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
09-25 20:23:31.755: W/System.err(19078):    at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
09-25 20:23:31.755: W/System.err(19078):    at org.htmlcleaner.Serializer.writeToFile(Serializer.java:131)
09-25 20:23:31.755: W/System.err(19078):    at org.htmlcleaner.Serializer.writeToFile(Serializer.java:142)
09-25 20:23:31.755: W/System.err(19078):    at com.whizzapps.stpsurniki.ScheudeleWithDesign$cleanHtml.doInBackground(ScheudeleWithDesign.java:36)
09-25 20:23:31.755: W/System.err(19078):    at com.whizzapps.stpsurniki.ScheudeleWithDesign$cleanHtml.doInBackground(ScheudeleWithDesign.java:1)
09-25 20:23:31.755: W/System.err(19078):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-25 20:23:31.755: W/System.err(19078):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-25 20:23:31.755: W/System.err(19078):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-25 20:23:31.755: W/System.err(19078):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-25 20:23:31.755: W/System.err(19078):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-25 20:23:31.755: W/System.err(19078):    at java.lang.Thread.run(Thread.java:841)
09-25 20:23:31.755: W/System.err(19078): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
09-25 20:23:31.755: W/System.err(19078):    at libcore.io.Posix.open(Native Method)
09-25 20:23:31.755: W/System.err(19078):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-25 20:23:31.763: W/System.err(19078):    at libcore.io.IoBridge.open(IoBridge.java:393)
4

1 回答 1

0

AsncTask 被执行。但是执行并没有因为异常而结束。

FileNotFoundEcxeption你在这一行得到一个:

 new PrettyXmlSerializer(props).writeToFile(node, "cleaned.xml", "utf-8");

它被你的 catch-block 捕获,防止你的应用程序崩溃。

异常可能由以下原因引起:

  • “cleaned.xml”文件的路径不正确。
  • 文件名中的错别字。
于 2013-09-25T18:40:33.247 回答