2012-12-28 27 views
5

ठीक है अच्छी तरह से मैं Jsoup का उपयोग कर दूरस्थ यूआरएल से एचटीएमएल पार्स करने के लिए उपयोग किया गया है साथ Jsoup का उपयोग कर:एंड्रॉयड - android_asset html फ़ाइल

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 है। आप अपनी फ़ाइल के लिए एक InputStream प्राप्त करने के लिए AssetManager का उपयोग करें और इसका इस्तेमाल कर सकते हैं:

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 एपीआई डॉक्स पढ़ लिया है चाहिए: रों – Neil