Android: Comic app development notes from WAP website to resolve picture address and load image

Source: Internet
Author: User

I. Using Jsoup to parse Web pages

<pre><code class= "Prettyprint" ><span class= "Typ" >document</span><span class= "PLN" > Doc </span><span class= "pun" >=</span><span class= "PLN" > </span><span class= "Typ" >jsoup</span><span class= "pun" >.</span><span class= "PLN" >connect</span><span class= "pun" > (</span><span class= "str" >url</span><span class= "pun" >) .</span>< Span class= "KWD" >get</span><span class= "pun" > (); </span><span class= "PLN" ></span> </code>

Second, the session remains

In the project, the browser to access the URL to click on the "next page" to turn the page, and the Android client each get "next" address is the first page. It turns out that when an Android client sends an HTTP request to a Web site and does not set Jsessionid on the request header, while using a browser to access the server, the Jsessionid is set in the cookie header each time the client initiates the request. Therefore, you can obtain the value of SessionID at the first data request and save it in a static variable, and then package it in a cookie for subsequent HTTP requests to the server, and the server gets the corresponding session based on this jsessionid. Instead of recreating a new session. Of course, you can also directly package the cookies that were obtained when the first data request is complete:

<span style= "FONT-SIZE:12PX;" ></span><pre name= "code" class= "java" ><span style= "font-size:12px;" >connection.response res= jsoup.connect (URL)         . Method (Method.get). Execute ();</span>
Document doc= jsoup.connect (URL). Cookies (Res.cookies ()). Timeout (10000). get ();


Third, the label analysis

Because the project site needs to parse the picture label unique width=100% This attribute, so you can parse out the image address and load the picture in the following ways:

<span style= "FONT-SIZE:12PX;" > Elements element = Doc.select ("img"). Select ("[width=100%]");           Link log.d with href attribute ("element", element.tostring ());           for (Element links:element) {String link = links.attr ("src");           try {myfileurl = new URL (link);           } catch (Malformedurlexception e) {log.i ("msg", "no Data");            } try {HttpURLConnection conn = (httpurlconnection) myfileurl.openconnection ();                        Conn.setconnecttimeout (0);                        Conn.setdoinput (TRUE);                        Conn.connect ();                        InputStream is = Conn.getinputstream ();                        Bitmap = Bitmapfactory.decodestream (IS);               Is.close ();               } catch (IOException e) {log.i ("msg", "no Data");                        } piclistinfo pi = new Piclistinfo ();                    Pi.setpicurl (bitmap);    Newpiclistinfo.add (0,PI); }</span>

Four, listview+adapter display pictures

<span style= "FONT-SIZE:12PX;" >if (newpiclistinfo.size () > 0) {collections.reverse (newpiclistinfo);        Piclistinfo.addall (Newpiclistinfo); sla.notifydatasetchanged ();p iclist.setdividerheight (0); if (dlg_ Loading.isshowing ()) {Dlg_loading.cancel ();}} ELSE{LOG.I ("msg", "no Data");}  </span>
because the site only shows 4 images per page, a total of 8 pages, so after loading the top four, you need to keep the session and simulate the browser's "page":

<span style= "FONT-SIZE:12PX;" >if (count<8) {      Element Next=doc.getelementbyid ("Tonext");      String next_url=next.attr ("href");//"next page" URL      getpic (next_url);//Call picture loaded async thread      }   else{   log.i ("msg "," load Complete ");   Toast.maketext (Getapplicationcontext (), "Loaded All",      Toast.length_short). Show ();   </span>
full thread loaded with Picture:

<span style= "FONT-SIZE:12PX;" >public void getpic (String url) {new Htmlasynctask (). Execute (URL);} private class Htmlasynctask extends asynctask<    String, Integer, arraylist<piclistinfo>> {Document doc = null; Protected arraylist<piclistinfo> doinbackground (String ...         URLs) {URL myfileurl = null;               Bitmap Bitmap = null;    arraylist<piclistinfo> newpiclistinfo = new arraylist<piclistinfo> ();    try {String test=res.cookies (). toString ();            Doc= Jsoup.connect (Urls[0]). Cookies (Res.cookies ()). Timeout (10000). get (); Elements element = Doc.select ("img"). Select ("[width=100%]");           Link log.d with href attribute ("element", element.tostring ());           for (Element links:element) {String link = links.attr ("src");           try {myfileurl = new URL (link);           } catch (Malformedurlexception e) {log.i ("msg", "no Data"); } try {HttpURLConnection conn = (httpurlconnection) myfileurl.openconnection ();                    Conn.setconnecttimeout (0);                    Conn.setdoinput (TRUE);                    Conn.connect ();                    InputStream is = Conn.getinputstream ();                    Bitmap = Bitmapfactory.decodestream (IS);               Is.close ();               } catch (IOException e) {log.i ("msg", "no Data");                   } piclistinfo pi = new Piclistinfo ();                   Pi.setpicurl (bitmap);                                   Newpiclistinfo.add (0,PI);    }}catch (IOException e) {//TODO auto-generated catch block log.i ("msg", "no Data");}                return newpiclistinfo; }//Async method call to process the returned string protected void OnPostExecute (arraylist<piclistinfo> newpiclistinfo) {try {count++  ;    if (newpiclistinfo.size () > 0) {collections.reverse (newpiclistinfo); Piclistinfo.addall (newpiclistinfo); sla.notifydAtasetchanged ();p iclist.setdividerheight (0), if (dlg_loading.isshowing ()) {Dlg_loading.cancel ();}}     ELSE{LOG.I ("msg", "no Data");}      if (count<8) {Element Next=doc.getelementbyid ("Tonext");      String next_url=next.attr ("href");      Getpic (Next_url);   } else{log.i ("msg", "Loaded Complete");   Toast.maketext (Getapplicationcontext (), "Loaded All", Toast.length_short). Show ();                     }} catch (Exception e) {toast.maketext (Getapplicationcontext (), "Network connection Error", Toast.length_short). Show (); }} </span>



Android: Comic app development notes from WAP website to resolve picture address and load 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.