Android App Development-Wizards CSDN Blog client's get comment list

Source: Internet
Author: User

Android App Development-Wizards CSDN Blog client's get comment list
The previous blog introduces the details of the blog's Business logic implementation, this blog introduces the last feature of the CSDN blog client, to get a list of comments, the implementation of this feature is not the same as the previous access to the article list and the detailed content of the article, CSDN Blog to get comments through JS to request the server to load the comments list, return data as JSON data, we have to do is to find such a JS file, then find the request URL splicing string, and then according to our needs, Request the comment list of the article to get the comment JSON data for the current article, then parse the work, and finally show it to our interface. If you do not carefully analyze the HTML code of the children's shoes, may not find this, the wizard in the development of this client, the moment also get a list of comments, and then through the exchange with CSDN technology, I looked carefully before I found the blog request Way, The use of Jsoup can not simulate the loading of JavaScript, so you can only view the JS code by themselves, find the URL of the request, the following author will tell you how to do this thing.
Wizards here to find a blog post with comments, such as the following: http://blog.csdn.net/wwj_748/article/details/39726051

we can see the bottom of this article is a list of our article comments, others have commented on their own reply. In the same way, F12 view the source code, or view the element to the comment content as follows:


then we click to see the corresponding JS file, to see if we can find what we want:
Oh, I was so careless that I found what I wanted:

from the above we can analyze that getting the comment list of an article requires a request similar to the following address:"http://blog.csdn.net/wwj_748/comment/list/39726051?page=1, at the beginning of the wizard did not know such a request address, is through the above way to know." We request an article to know the filename and pageindex of the corresponding article, and then splice it in this form:
/** * Back to post comment List link *  * @param filename *            filename * @param pageIndex *            pages * @return */public static String getcomment listURL (string filename, string pageIndex) {return "http://blog.csdn.net/wwj_748/comment/list/" + filename+ "? page=" + PageIndex;}

To this point basically solves the most troublesome thing, the following is the implementation of the business logic:/blogclient/src/com/xiaowu/blogclient/blogcommentactivity.java
Package Com.xiaowu.blogclient;import Java.util.list;import Me.maxwin.view.ixlistviewloadmore;import Me.maxwin.view.ixlistviewrefreshlistener;import Me.maxwin.view.xlistview;import Android.app.Activity;import Android.os.asynctask;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.window;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.imageview;import Android.widget.ProgressBar ; import Android.widget.textview;import Android.widget.toast;import com.xiaowu.blogclient.adapter.CommentAdapter; Import Com.xiaowu.blogclient.model.comment;import Com.xiaowu.blogclient.model.page;import Com.xiaowu.blogclient.util.constants;import Com.xiaowu.blogclient.util.dateutil;import Com.xiaowu.blogclient.util.httputil;import Com.xiaowu.blogclient.util.jsouputil;import com.xiaowu.blogclient.util.urlutil;/** * 2014/8/13 * * Blog Comments list * * @author wwj_748 * */public class Blogcommentactivity ExTends Activity Implementsixlistviewrefreshlistener, ixlistviewloadmore {private Xlistview listview;private Commentadapter adapter;private ProgressBar progressbar;private ImageView reloadimageview;private ImageView backbtn; Private TextView commenttv;public static string commentcount = "";p rivate page page;private String filename;private int PA Geindex = 1;private int pageSize =: @Overrideprotected void onCreate (Bundle savedinstancestate) {requestwindowfeature ( Window.feature_no_title); super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_comment); init (); InitComponent (); Listview.setrefreshtime (Dateutil.getdate ()); Set refresh Time Listview.startrefresh (); Start refresh}//Initialize private void init () {filename = Getintent (). Getextras (). getString ("filename");//Get file Name page = new page (); Ada Pter = new Commentadapter (this);} Initializes the component private void InitComponent () {ProgressBar = (ProgressBar) Findviewbyid (r.id.newscontentpro); Reloadimageview = ( ImageView) Findviewbyid (r.id.reloadimage); Reloadimageview.Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {System.out.println ("click"); Reloadimageview.setvisibility (view.invisible);p rogressbar.setvisibility (view.visible); new MainTask (). Execute ( Constants.def_task_type. REFRESH);}); BACKBTN = (ImageView) Findviewbyid (R.ID.BACKBTN); Backbtn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {finish ();}}); Commenttv = (TextView) Findviewbyid (r.id.comment); ListView = (Xlistview) Findviewbyid (R.id.listview); Listview.setadapter (adapter); listview.setpullrefreshenable (this); listview.setpullloadenable (this); Listview.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> Parent, View view,int position, long ID) {}}); @Overridepublic void Finish () {super.finish ();//Exit Animation overridependingtransition (R.anim.push_no, R.anim.push_right_ Out);} Private class Maintask extends Asynctask<string, Void, integer> {@Overrideprotected Integer doinbackground (strinG ... params) {//Get return JSON strings string temp = Httputil.httpget (Urlutil.getcommentlisturl (Filename,page.getcurrentpage ()) if (temp = = null) {return constants.def_result_code. ERROR;} Get list of comments list<comment> list = Jsouputil.getblogcommentlist (Temp,integer.valueof (Page.getcurrentpage ()), PageSize); if (list.size () = = 0) {return Constants.DEF_RESULT_CODE.NO_DATA;} if (Params[0].equals (Constants.def_task_type. LOAD) {adapter.addlist (list); return Constants.def_result_code. LOAD;} else {adapter.setlist (list); return Constants.def_result_code. REFRESH;}} @Overrideprotected void OnPostExecute (Integer result) {if (result = = Constants.def_result_code. ERROR) {Toast.maketext (Getapplicationcontext (), "Poor network Signal", Toast.length_short). Show (); Listview.stoprefresh ( Dateutil.getdate ()); Listview.stoploadmore (); reloadimageview.setvisibility (view.visible);} else if (result = = Constants.DEF_RESULT_CODE.NO_DATA) {Toast.maketext (Getapplicationcontext (), "No more comments", toast.length_ Short). Show (); Listview.stoploadmore (); ListView. Stoprefresh (Dateutil.getdate ()); Commenttv.settext ("Total Comments:" + Commentcount);} else if (result = = Constants.def_result_code. LOAD) {page.addpage ();p ageindex++;adapter.notifydatasetchanged (); Listview.stoploadmore ();} else if (result = = Constants.def_result_code. REFRESH) {adapter.notifydatasetchanged (); Listview.stoprefresh (Dateutil.getdate ());p age.setpage (2); Commenttv.settext ("Total Comments:" + commentcount); Shows number of comments}progressbar.setvisibility (view.invisible); Super.onpostexecute (result);}} Load more @overridepublic void Onloadmore () {new Maintask (). Execute (constants.def_task_type. LOAD);} Refresh comments @overridepublic void Onrefresh () {page.setpage (1); new Maintask (). Execute (constants.def_task_type. REFRESH);}}

/blogclient/src/com/xiaowu/blogclient/adapter/commentadapter.java
Package Com.xiaowu.blogclient.adapter;import Java.util.arraylist;import Java.util.list;import Android.content.context;import Android.graphics.bitmap;import Android.text.html;import Android.text.spannablestringbuilder;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.textview;import Com.nostra13.universalimageloader.core.displayimageoptions;import Com.nostra13.universalimageloader.core.imageloader;import Com.nostra13.universalimageloader.core.imageloaderconfiguration;import Com.nostra13.universalimageloader.core.assist.imagescaletype;import Com.nostra13.universalimageloader.core.display.fadeinbitmapdisplayer;import Com.xiaowu.blogclient.R;import Com.xiaowu.blogclient.model.comment;import com.xiaowu.blogclient.util.constants;/** * Comment List Adapter * * @author wwj_748 * */p Ublic class Commentadapter extends Baseadapter {private Viewholder holder;private layoutinflater lAyoutinflater;private Context context;private list<comment> list;private spannablestringbuilder htmlSpannable; Private Imageloader Imageloader = Imageloader.getinstance ();p rivate displayimageoptions options;private String Replytext;public Commentadapter (Context c) {super (); layoutinflater = (Layoutinflater) layoutinflater.from (c); list = New Arraylist<comment> (); Imageloader.init (Imageloaderconfiguration.createdefault (c)); options = new Displayimageoptions.builder (). Showstubimage (R.DRAWABLE.CSDN). Showimageforemptyuri (R.DRAWABLE.CSDN). Showimageonfail (R.DRAWABLE.CSDN). Cacheinmemory (). Cacheondisc (). Imagescaletype (imagescaletype.exactly). Bitmapconfig (Bitmap.Config.RGB_565). Displayer (new Fadeinbitmapdisplayer). Build ();} public void setlist (list<comment> list) {this.list = list;} public void Addlist (list<comment> list) {this.list.addAll (list);} public void Clearlist () {this.list.clear ();} Public list<comment> getList () {return List;} public void RemoveItem (int positiON) {if (list.size () > 0) {list.remove (position);}} @Overridepublic int GetCount () {return list.size ();} @Overridepublic Object getItem (int position) {return list.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Comment item = list.get (position);//Get comments Item if (null = = Convertview) {holder = new Viewholder (); switch (Item.gettype ()) {case Constants.def_comment_type. Parent://Parental Convertview = Layoutinflater.inflate (r.layout.comment_item,null); holder.name = (TextView) Convertview.findviewbyid (r.id.name); holder.content = (TextView) Convertview.findviewbyid (r.id.content); Holder.date = (TextView) Convertview.findviewbyid (r.id.date); holder.reply = (TextView) Convertview.findviewbyid ( R.id.replycount); holder.userface = (ImageView) Convertview.findviewbyid (r.id.userface); Break;case Constants.DEF_ Comment_type. Child://subkey Convertview = Layoutinflater.inflate (R.layout.comment_child_item, NULL); holder.name = (TextView) Convertview.findviewbyid (r.id.name); holder.content = (TextView) Convertview.findviewbyid ( r.id.content); holder.date = (TextView) Convertview.findviewbyid (r.id.date); break;} Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} if (null! = Item) {switch (Item.gettype ()) {case Constants.def_comment_type. Parent://Subject Item Holder.name.setText (Item.getusername ()); Holder.content.setText (html.fromhtml (Item.getcontent ())); Show Comments Holder.date.setText (Item.getposttime ());//holder.reply.settext (Item.getreplycount ()); Imageloader.displayimage (Item.getuserface (), holder.userface, options);//display Avatar Break;case Constants.def_comment_ TYPE. Child://Reply Item Holder.name.setText (Item.getusername ()); replytext = Item.getcontent (). replace ("[Reply]", "" "); Replytext = Replytext.replace ("[/reply]", "" "); Holder.content.setText (html.fromhtml (Replytext)); Holder.date.setText (Item.getposttime ()); break;default:break;}} return Convertview;} @Overridepublic int Getviewtypecount () {return 2;} @Overridepublic int Getitemviewtype (int position) {switch (List.get (position). GetType ()) {case Constants.def_comment_ TYPE. Parent://Parental node return 0;case constants.def_comment_type. Child://Sub-node return 1;} return 1;} @Overridepublic boolean isenabled (int position) {return true;} Private class Viewholder {TextView id; TextView date; TextView name; TextView Content;imageview Userface; TextView reply;}}


The end result is as follows:

finally:about Little Witch csdn Blog client development is basically finished, more detailed implementation please go to the following link to download the Source view: http://download.csdn.net/detail/wwj_748/7912513

Android App Development-Wizards CSDN Blog client's get comment list

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.