Loading raw HTML in Java/Android
I have written a couple of 开发者_开发技巧live wallpapers in recent weeks using local resources. Now a potential client wants me to make one that loads and displays the photos (usually between 3 and 10) from his daily news report posted online. The report file has a URL along the lines of http://example.com/dailytext/report.html
which loads images along the lines of http://example.com/dailymedia/obama.jpg
The references in report.html
look like
img src="../dailymedia/obama.jpg" ...
Am I supposed to use a WebView to do this? That doesn't seem quite right, because I don't want to display the HTML. I would think that I want to throw the raw HTML into an array, parse the HTML looking for the instances of "img src..."
, reconstruct the full URLs, then load the bitmaps. I'm getting the impression this is more of a pure Java task than anything to do with Android's specialized classes, but I don't know. Any suggestions about "best practice?"
Unless I have misunderstood, this really isn't hard. You need to do the following:
- Fetch the HTML, using either the native java networking api or something like HttpClient
- Use a parser like Jericho or Dom4j to extract out the image links
- Construct the absolute URLs, can be done with just java.net.URL
- Fetch the images
You could also prefer using Jsoup HTML Parser.
精彩评论