5

好吧,我一直在使用 Jsoup 从远程 url 解析 html,使用:

Jsoup.connect(url).timeout(20000).get();

我现在正在尝试读取存储在assets文件夹中的本地 html 文件。我做了很多搜索,但找不到解决方案。在Jsoup 示例 - Load a Document from a File中,他们说要执行以下操作:

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

根据我的阅读,我的文件的路径是 - file:///android_asset/results_2009.html

在此处输入图像描述

但是我总是得到no such file or directory,那么如何将本地文件放入 Jsoup?

我需要使用AssetManager什么的吗?请有人指出我正确的方向。

4

1 回答 1

9

Jsoup.parse()有一个需要 InputStream 的重载。您可以使用AssetManager获取InputStream文件并使用它:

InputStream is=null;

try {
    is=getAssets().open("results_2009.html");
    Document doc = Jsoup.parse(is, "UTF-8", "http://example.com/");
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(is!=null)
        is.close();
}
于 2012-12-28T14:52:42.780 回答