Android App Development-Wizards CSDN Blog client's get comment list
The previous blog introduces the specific content of the blog's Business logic implementation, this blog introduces the wizard csdn blog client's last feature. Get a list of comments, the implementation of this feature is not the same as the previous to get the article list and article specific content, CSDN blog to get comments through JS to request the server to load the comments list, the return data is JSON data. What we have to do here is to find this one JS file, and then find the stitching string for the request URL. Then according to our needs, request the comment list of the article to get the comment JSON data of the current article, and then do the parsing work. Finally, we show our interface. Suppose you do not have a detailed analysis of the HTML code of children's shoes. May not find this, the wizard in the development of this client, the moment also get a list of comments, and later through the exchange with the CSDN technology, I looked carefully to find out about the blog request way. The use of Jsoup can not be used to simulate the loading of JavaScript, so only through their own view of the JS code, find the URL of the request, the following author will tell you how to do this thing.
The wizard is looking for a blog post with comments. For example, the following article: http://blog.csdn.net/wwj_748/article/details/39726051
We can see the bottom of this article is a list of our articles 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 seen in the following:
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 the. Get the comment list of the article need to request similar to the following address:"http://blog.csdn.net/wwj_748/comment/list/39726051?"
Page=1. Just started the wizard did not know this request address, is through the above way to know. We ask for an article to know the filename and pageindex of the corresponding article, and then the following side of this form splicing:
/** * Back to post comment List link * * @param filename * file name * @param pageIndex * pages * @return */public static String Getcomm Entlisturl (string filename, string pageIndex) {return "http://blog.csdn.net/wwj_748/comment/list/" + filename+ "? page= "+ PageIndex;}
By this step, we have basically conquered the most troublesome things. 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 (); ad Apter = 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) {//Gets the return JSON string, "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 many other comments", Toast.length_short). Show (); Listview.stoploadmore (); ListvIew.stoprefresh (Dateutil.getdate ()); Commenttv.settext ("co-owned 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 ("co-owned comments:" + commentcount); Shows number of comments}progressbar.setvisibility (view.invisible); Super.onpostexecute (result);}} Loads many other @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;}}
finally for example the following:
finally:about the wizard CSDN Blog client development is basically finished, a lot of other specific implementation please go to the following link to download the source Code view: http://download.csdn.net/detail/wwj_748/7912513
Android App Development-Wizards CSDN Blog client's get comment list