2012-12-28 34 views
5

玉以及我一直在使用Jsoup使用解析从远程URL的HTML android_asset HTML文件:Android的 - 使用Jsoup与

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

我现在想读我已经存储在assets文件夹本地HTML文件。我做了很多搜索,但我找不到解决方案。在Jsoup example - 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

enter image description here

不过,我总是得到no such file or directory,让我怎么得到一个本地文件到Jsoup?

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

回答

9

Jsoup.parse()有一个overload which takes an 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(); 
} 
+0

想我应该读Jsoup API文档:■ – Neil