Android: cartoon APP development notes-Parse image addresses from WAP websites and load images

Source: Internet
Author: User

Android: cartoon APP development notes-Parse image addresses from WAP websites and load images
1. Use Jsoup to parse webpages

Document doc = Jsoup.connect(url).get();

Ii. Session persistence

In the project, you can access the URL through a browser and click "next page" to flip the page normally. The "next page" address obtained by the Android client each time is the first page. It turns out that when the android client sends an HTTP request to the website, JSESSIONID is not set in the request header. When a browser is used to access the server, JSESSIONID will be included in the Cookie header. Therefore, you can obtain the sessionid value in the first data request and save it in a static variable. Then, you can package it in the Cookie of the subsequent HTTP request and send it to the server, the server obtains the corresponding Session based on the jsessionid instead of creating a new Session. Of course, you can also directly package the cookies obtained during the first data request back:

Connection.Response res= Jsoup.connect(url)         .method(Method.GET) .execute();
Document doc = Jsoup. connect (url). cookies (res. cookies (). timeout (10000). get ();


Iii. Tag Parsing

Because the Image Tag that the project website needs to parse has a special width = 100% attribute, You can parse the image address and load the image in the following way:

Elements element = doc. select ("img "). select ("[width = 100%]"); // link Log with the href attribute. d ("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 );}

Iv. listview + adapter display image

If (newpiclistinfo. size ()> 0) {Collections. reverse (newpiclistinfo); piclistinfo. addAll (newpiclistinfo); sla. yydatasetchanged (); piclist. setDividerHeight (0); if (dlg_loading.isShowing () {dlg_loading.cancel () ;}} else {Log. I ("msg", "No data ");}
Since each page of the website only displays four images, there are a total of eight pages. Therefore, after loading the first four images, you need to maintain the session and simulate the "flip" of the browser ":

If (count <8) {Element next = doc. getElementById ("toNext"); String next_url = next. attr ("href"); // "next page" url getpic (next_url); // call the asynchronous thread for image loading} else {Log. I ("msg", "loaded"); Toast. makeText (getApplicationContext (), "All loaded", Toast. LENGTH_SHORT ). show ();}
Complete thread for attaching images:

Public void getpic (String url) {new htmlasynctask(.exe cute (url);} private class HTMLAsyncTask extends AsyncTask
   
    
> {Document doc = null; protected ArrayList
    
     
DoInBackground (String... Urls) {URL myFileUrl = null; Bitmap bitmap = null; ArrayList
     
      
Newpiclistinfo = new ArrayList
      
       
(); 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 with the href attribute. d ("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;} // calls an asynchronous method to process the returned string protected void onPostExecute (ArrayList
       
        
Newpiclistinfo) {try {count ++; if (newpiclistinfo. size ()> 0) {Collections. reverse (newpiclistinfo); piclistinfo. addAll (newpiclistinfo); sla. yydatasetchanged (); piclist. 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"); Toast. makeText (getApplicationContext (), "All loaded", Toast. LENGTH_SHORT ). show () ;}} catch (Exception e) {Toast. makeText (getApplicationContext (), "network connection error", Toast. LENGTH_SHORT ). show ();}}}
       
      
     
    
   



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.