Android uses Jsoup to parse html+ download image

Source: Internet
Author: User

Recently wanted to tinker with the CSDN client, this blog mainly describes how to use the Jsoup parsing HTML page to get the desired content through the tag, and download the specified picture resources.

First, import Jsoup jar Package

Jar Package: Jsoup 1.6.1

Note When importing the package to the project, copy the extracted jar files to the Libs file directory directly, or the runtime will error.




Second, download the HTML page and parse

Code:

Package Com.example.testcsdn;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.url;import Java.util.ArrayList;import Org.jsoup.jsoup;import Org.jsoup.nodes.document;import Org.jsoup.nodes.element;import org.jsoup.select.Elements; import android.util.log;/** * resolves the acquired HTML resource by a given link address, returning the encapsulated Arraylist<blog> object */public class BLOGSFETCHR {private Static final String TAG = "BLOGSFETCHR";/** * Download URL specified resource * * @return returned as type byte[] * */public byte[] geturlbytes (String URL Spec) throws IOException {URL url = new URL (urlspec); HttpURLConnection conn = (httpurlconnection) url.openconnection ();//cast here, This is because the httpurlconnection.getinputstreamtry {bytearrayoutputstream out = new Bytearrayoutputstream () is used below; InputStream in = Conn.getinputstream (); if (Conn.getresponsecode ()! = HTTPURLCONNECTION.HTTP_OK) {//Connection unsuccessful log.i (TAG, "Connection unsuccessful"); return null;} byte[] buffer = new Byte[1024];int len = 0;while (len = in.read (buffer)) > 0) {out.write (buffer, 0, Len);} Out.close (); return Out.tobytearray ();} finally {conn.disconnect ();}} /** * Download URL specifies the resource (the return value of the Geturlbytes method byte[] is converted to a string type) * * @return return type is string */private string GetUrl (String urlspec) {S Tring result = Null;try {result = new String (geturlbytes (Urlspec)),} catch (IOException e) {e.printstacktrace ();} return result;} Public arraylist<blog> Downloadblogitems (String urlspec) {arraylist<blog> blogs = new arraylist<> (); String htmlstring = GETURL (URLSPEC);//parse Htmlstringparseritems (blogs, htmlstring); return blogs;} private void Parseritems (arraylist<blog> blogs, String htmlstring) {Document doc = Jsoup.parse (htmlstring); Elements units = Doc.getelementsbyclass ("Blog_list"); for (int i = 0; i < units.size (); i++) {Blog blog = new Blog (); Element Unit_ele = Units.get (i); Element Dl_ele = Unit_ele.getelementsbytag ("DL"). Get (0); Element Dl_dt_ele = Dl_ele.getelementsbytag ("DT"). Get (0); Element Dt_a_ele = dl_dt_ele.child (0); String ICONURL = Dt_a_ele.child (0). attr ("src"); Bo Master avatar Link log.i (TAG, "article" + i + "Bo Master Avatar Link:" + iconUrl); Elements FLS = Unit_ele.getelementsbyclass ("fl"); Element Fl_ele = fls.get (0); Element Fl_a1_ele = fl_ele.child (0); String Bloggerid = Fl_a1_ele.text (); Bo Master idlog.i (TAG, "article" + i + "+ Bloggerid); Blog.setbloggericonurl (ICONURL); Blog.setbloggerid (Bloggerid); Blogs.add ( blog);}}}

As the code shows, parsing HTML with Jsoup is straightforward.

You can use the browser, right-click to review the elements, get the tool box shown, you can quickly find the label of the elements in the page, and then use the Jsoup API to get the value of the tag.




Third, download the specified picture

If you want to download a sub-item in the blog list, Blogger's avatar. You can get the URL of the image by parsing the HTML before using httpurlconnection to download it directly.

The following creates a thumbnaildownloader<token> class that inherits Handlerthread to wait for and process the picture download request while updating the UI:

Package Com.example.testcsdn;import Java.io.ioexception;import Java.util.collections;import java.util.HashMap; Import Java.util.map;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.handler;import Android.os.handlerthread;import Android.os.message;import Android.support.v4.util.lrucache;import Android.util.log;import Android.widget.imageview;public Class Thumbnaildownloader<token> extends Handlerthread {//token denotes generics, class name < generics > to ensure that tokens can be used within the class, Just like token is already a defined class private static final String TAG = "Thumbnaildownloader";p rivate static final int message_download = 0;pri Vate Handler Mhandler; Send the instructions to download the picture, and handle the instructions to download the image of the messenger private Handler Mresponsehandler; Handler from the main thread, update uiprivate listener<token> mlistener;private map<token, string> requestmap = Collections.synchronizedmap (New Hashmap<token, string> ());//Save key-value pairs for ImageView and URLs, and are thread-safe private lrucache< String, bitmap> mmemorycache;//The class of the cached picture, when the size of the stored picture is greater than the value set by LRUCache, the system automatically frees the memory public ThUmbnaildownloader (Handler Handler) {super (tag); Mresponsehandler = handler;//Create a handlerthread named TAG, is the independent thread that has its own looper//super (TAG) equals the new Handlerthread (tag) int maxmemory = (int) runtime.getruntime (). MaxMemory (); System max run memory int mcachesize = MAXMEMORY/8; Memory size allocated to cache Mmemorycache = new lrucache<string, bitmap> (mcachesize) {//must override this method to measure Bitmap size @overrideprotected int sizeOf (String key, Bitmap value) {return value.getrowbytes () * Value.getheight ();}};} Public interface Listener<token> {///callback method, implement void onthumbnaildownloaded (token token, Bitmap thumbnail) in the main thread;} public void Setlistener (listener<token> Listener) {mlistener = Listener;} @Overridepublic void onlooperprepared () {//Looper to start the Cycle Prep window on this thread mhandler = new Handler () {//new Handler in the current thread, Only run @overridepublic void handlemessage (Message message) {//process the sent picture download message, download the picture and update the UIIF (message.what = = Message_) only on the current thread. DOWNLOAD) {Token token = (token) message.obj;try {handlerequest (token);//Processing message} catch (IOException e) {E.printstacKtrace ();}}};} private void HandleRequest (final token token) throws IOException {final String url = requestmap.get (Token); if (url = = NULL ) return;byte[] bitmapbytes = new BLOGSFETCHR (). geturlbytes (URL);//download image final Bitmap Bitmap = Bitmapfactory.decodebytearray (Bitmapbytes, 0,bitmapbytes.length); String key = (String) ((ImageView) token). Gettag (); LOG.I (tag, "ImageView tag is:" + key); Mmemorycache.put (key, bitmap); Cache Mresponsehandler.post (New Runnable () {@Overridepublic void Run () {//Update uiif (Requestmap.get (token)! = URL) return; Requestmap.remove (token); mlistener.onthumbnaildownloaded (token, bitmap);//Update UI}}); public void Clearqueue () {mhandler.removemessages (message_download); Requestmap.clear ();} public void Queuethumbnail (token token, String URL) {//The Download Picture command is added to the "Thumbnaildownloader" message queue,// Called Requestmap.put (token, url) in photogalleryfragment; Message message = Mhandler.obtainmessage (message_download, token);//Get message and automatically bind with Mhandler//parameter one: What,int type, Used to describe the message//parameter two: obj, the specified object sent with the message//parameter three: Target, which handles the handler of the message, is the default Message.sendtotarget () due to the use of automatic and mhandler bindings; Send message to target handler}public Bitmap getcacheimage (String key) {//Get picture in cache Bitmap Bitmap = Mmemorycache.get (key); return Bitmap;}}


Mainactivity:

Package Com.example.testcsdn;import Java.util.arraylist;import Android.app.activity;import android.graphics.Bitmap ; Import Android.os.asynctask;import Android.os.bundle;import android.os.handler;import Android.util.Log;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.imageview;import Android.widget.listview;import Android.widget.textview;public class MainActivity Extends Activity {private static final String TAG = "mainactivity";p rivate ListView mlistview;private ARRAYLIST&LT;BLOG&G T Mblogs; Blog list private String Testurl = "http://blog.csdn.net/column.html"; Visit the link here to test the CSDN blog column on the home page of private BLOGSFETCHR FETCHR; Download the HTML page and parse its tool object private Myadapter adapter;private thumbnaildownloader<imageview> Mthumbnaildownloader; Picture Downloader @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); fetchr = new BLOGSFETCHR (); mblogs = new arraylist<blog> (); LOG.I (TAG, "mblogs.size:" + mblogs.size ()); Blog blog = new Blog (); Blog.setbloggerid ("Hello"); mblogs.add (blog); update (testurl);// Open the response download picture message thread Mthumbnaildownloader = new thumbnaildownloader<imageview> (new Handler ()); Mthumbnaildownloader.setlistener (New thumbnaildownloader.listener<imageview> () {@Overridepublic void onthumbnaildownloaded (ImageView imageview,bitmap thumbnail) {//update ui,imageview.setimagebitmap (thumbnail);}}); Mthumbnaildownloader.start (); Mthumbnaildownloader.getlooper (); Must be}private void update after start (final String testurl) {new asynctask<void, void, void> () {@Overrideprotected Voi D doinbackground (Void ... params) {mblogs = Fetchr.downloadblogitems (Testurl);//download blog list return null;}; @Overrideprotected void OnPostExecute (void result) {//Update Listviewmlistview = (ListView) Findviewbyid (r.id.listview_ Blogcolumn) adapter = new Myadapter (mblogs); Mlistview.setadapter (adapter);}}. Execute ();} Private class Myadapter extends arrayadapter<blog> {public myadapter(Arraylist<blog> blogs) {Super (mainactivity.this, 0, blogs);} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {if (Convertview = = null) {Convertview = ge Tlayoutinflater (). Inflate (R.layout.listview_item, null);} ImageView ImageView = (ImageView) Convertview.findviewbyid (R.id.imageview); TextView TextView = (TextView) Convertview.findviewbyid (R.id.textview); Textview.settext (GetItem (position). Getbloggerid ()); String imageUrl = GetItem (position). Getbloggericonurl (); String imagetag = Imageurl.replaceall ("[^\\w]", "" "); Imageview.settag (Imagetag);//Remove non-string (letters, numbers, underscores)// Set a label for ImageView for access to Cachebitmap bitmap = Null;if ((bitmap = Mthumbnaildownloader.getcacheimage (Imagetag))! = null) {/ /if Imageview.setimagebitmap (bitmap) exists in the cache;} else {//Send download Picture message mthumbnaildownloader.queuethumbnail (ImageView, IMAGEURL);} return Convertview;}}}

Operating Effect:



SOURCE download


Android uses Jsoup to parse html+ download image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.