Android Application Development-Get Comments list from The Little Wu CSDN blog client, androidcsdn
Android Application Development-Get Comments list from xiaowu CSDN blog Client
The previous blog introduced the detailed business logic Implementation of the blog post. This blog introduces the last function of the client of the Little Wu CSDN blog to obtain the comment list, the implementation of this function is different from the previous article list and detailed content. The CSDN blog requests the server to load the comment list through js, and the returned data is json data, what we need to do here is to find such a js file, then find the concatenated string of the request url, and then according to our needs, request the comment list of the article to obtain the json comment data of the current article, parse it, and display it on our interface. If you do not carefully analyze the html code, you may not be able to find this point. When you develop this client, Xiao Wu cannot get a comment list at the moment. After communicating with CSDN technology, I checked the blog request method carefully. Here, jsoup cannot simulate javascript loading, so I can only view the js Code and find the request url by myself, I will tell you how to do this.
Wu here to find a blog with a comment, such as the following: http://blog.csdn.net/wwj_748/article/details/39726051
We can see that at the bottom of this article is the list of comments of our article, and others have their own replies. In the same way, F12 can view the source code or view elements to locate the comment content, as shown below:
At this time, we click to view the corresponding js file to see if we can find what we want:
Oh, I accidentally discovered what I wanted:
From the above we can analyze, to get the comment list of the article needs to request the address similar to the following: "http://blog.csdn.net/wwj_748/comment/list/39726051? Page = 1. At the beginning, Xiao Wu did not know such a request address. It was learned through the above method. We request an article to know the filename and pageIndex of the corresponding article, and then splice them in the following form:
/*** Return to the blog comment List link ** @ param filename * file name * @ param pageIndex * page number * @ return */public static String getCommentListURL (String filename, String pageIndex) {return "http://blog.csdn.net/wwj_748/comment/list/" + filename + "? Page = "+ pageIndex ;}
This step basically solves the most troublesome problem. 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. widge T. 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. B Logclient. util. httpUtil; import com. xiaowu. blogclient. util. jsoupUtil; import com. xiaowu. blogclient. util. URLUtil;/*** 2014/8/13 ** blog comment list ** @ author wwj_748 **/public class BlogCommentActivity extends Activity example, IXListViewLoadMore {private XListView listView; private CommentAdapter adapter; private ProgressBar progressBar; private ImageView reLoadImageView; private ImageView backBtn; private TextView commentTV; public static String commentCount = ""; private Page page; private String filename; private int pageIndex = 1; private int pageSize = 20; @ Overrideprotected void onCreate (Bundle savedInstanceState) {requestWindowFeature (Window. FEATURE_NO_TITLE); super. onCreate (savedInstanceState); setContentView (R. layout. activity_comment); init (); initComponent (); listView. setRef ReshTime (DateUtil. getDate (); // set the refresh time listView. startRefresh (); // start to refresh} // initialize private void init () {filename = getIntent (). getExtras (). getString ("filename"); // get the file name page = new Page (); adapter = new CommentAdapter (this);} // initialize the private void initComponent () Component () {progressBar = (ProgressBar) findViewById (R. id. newsContentPro); reLoadImageView = (ImageView) findViewById (R. id. reLoadImage); reLoadImageView. setO NClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {System. out. println ("click"); reLoadImageView. setVisibility (View. INVISIBLE); progressBar. setVisibility (View. VISIBLE); new maintask(cmd.exe cute (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 the animation overridePendingTransition (R. anim. push_no, R. anim. push_right_out);} private class MainTask extends AsyncTask <String, Void, Integer >{@ Overrideprotected Integer doInBackground (String... params) {// obtain the returned json String temp = HttpUtil. httpGet (URLUtil. getCommentListURL (filename, page. getCurrentPage (); if (temp = null) {return Constants. DEF_RESULT_CODE.ERROR;} // obtain the Comment 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 ("Comments:" + commentCount);} else if (result = Constants. DEF_RESULT_CODE.LOAD) {page. addPage (); pageIndex ++; adapter. yydatasetchanged (); listView. stopLoadMore ();} else if (result = Constants. DEF_RESULT_CODE.REFRESH) {adapter. yydatasetchanged (); listView. stopRefresh (DateUtil. getDate (); page. setPage (2); commentTV. setText ("Comments:" + commentCount); // display comments} progressBar. setVisibility (View. INVISIBLE); super. onPostExecute (result) ;}}// load more @ Overridepublic void onLoadMore () {new maintask(cmd.exe cute (Constants. DEF_TASK_TYPE.LOAD);} // refresh comments @ Overridepublic void onRefresh () {page. setPage (1); new maintask(cmd.exe cute (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. textVi Ew; 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. blogclien T. model. comment; import com. xiaowu. blogclient. util. constants;/*** comment list adapter ** @ author wwj_748 **/public class CommentAdapter extends BaseAdapter {private ViewHolder holder; private LayoutInflater layoutInflater; private Context context Context; private List <Comment> list; private SpannableStringBuilder htmlSpannable; private ImageLoader imageLoader = ImageLoader. getInstance (); private DisplayImageOptions optio Ns; 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 (). image ScaleType (ImageScaleType. EXACTLY ). bitmapConfig (Bitmap. config. RGB_565 ). displayers (new FadeInBitmapDisplayer (300 )). 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. r Emove (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 if (null = convertView) {holder = new ViewHolder (); switch (item. getTyp E () {case Constants. DEF_COMMENT_TYPE.PARENT: // parent 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 = (ImageVie W) convertView. findViewById (R. id. userface); break; case Constants. DEF_COMMENT_TYPE.CHILD: // subitem 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 {hold Er = (ViewHolder) convertView. getTag ();} if (null! = Item) {switch (item. getType () {case Constants. DEF_COMMENT_TYPE.PARENT: // theme item holder. name. setText (item. getUsername (); holder. content. setText (Html. fromHtml (item. getContent (); // display the comment content holder. date. setText (item. getPostTime (); // holder. reply. setText (item. getReplyCount (); imageLoader. displayImage (item. getUserface (), holder. userface, options); // display the 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: // return 0 for the parent node; case Constants. DEF_COMMENT_TYPE.CHILD: // subnode 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 final result is as follows:
Finally: about Little Wu CSDN blog client development are basically introduced, more detailed implementation Please download the source code to view the following link: http://download.csdn.net/detail/wwj_748/7912513
Android Application Development (version 2nd)
The author not only has rich Android development experience, but also has published monographs in this area, with high sales and comments. The book uses the basic theory + practical cases + advanced skills as the structure of the content, especially the book uses a classic comprehensive case Sina Weibo client throughout the book, the entire case development process is completely reproduced in an iterative manner, which is extremely operational. Coupled with the previous basic theories and advanced skills, the reader can integrate the theoretical knowledge while practicing it. The main content of the book is divided into three parts: the basic article briefly describes the basic knowledge of Android development, and laid the foundation for the content of the second part of the example.
Android Application Development practices is a reference book for Android Application Development that closely integrates practices and theories. The Practice section uses a complete large-scale case (a fully functional Weibo client) to demonstrate and explain the entire process of the Case Development in an iterative manner, it aims to help readers quickly clarify the complete process and implementation details of Android Application Development, and at the same time, it gives a detailed analysis and explanation of the theoretical knowledge involved in the development process. The theoretical part is the sublimation of the practical part, and the advanced knowledge and common skills required for Android Application Development are elaborated in depth. After understanding this part, after quickly gaining practical application development experience, we can further improve our theoretical and technical skills.
The book is divided into three parts. The first part is the basic article. It comprehensively introduces the Android system architecture, the establishment of the development environment, common components of Android applications, and a simple implementation method of Weibo client, this laid the foundation for implementing the complete case (Sina Weibo client) in this book. The second part is an example of the overview of Weibo client and Sina Weibo API, then, based on the function division of the Weibo client, the implementation methods and details of each function module of the Weibo client are described in detail, including interface display and code analysis, as well as a key explanation of the theoretical knowledge used, it is easy for readers to practice and helps them consolidate their theoretical knowledge. Part 3: Advanced Technology in Android Development, including development of various commonly used Android resources and communication functions, databases, Bluetooth and Wi-Fi, third-party libraries, 2D drawing technology, OpenGL ES drawing technology, and Android compilation, and Android performance optimization methods and practices. Finally, we will introduce the new features of Android 4.0 in detail.
Li Ning is a Senior Software Development Engineer, IT lecturer, and technical consultant. He has been engaged in software R & D for more than one year and has rich practical experience. Senior Android development expert, domestic Ar ~ Droid-Pioneer and missionary in application development. Developed ~ Android platform's Sina Weibo client, perfect communication, voice calculator, multi-function calendar and other popular applications. Active in GSDN (CSDN expert), blog Park, and other communities, serving as eoeAndro (d Development Forum moderator. I have published a large number of technical articles on Android Application Development, which are very popular among users in the community. In addition, he is also proficient in Java,. NET, C ++ and other technologies. He has won multiple technical competitions organized by Microsoft.
Is there any software for writing a csdn blog on android? Thank you!
We recommend that you use the mobile QQ browser (Android version), the unique forum mode, which allows you to quickly browse and update your blog. Mb.qq.com/android/