Android Application Development-Get Comments list from xiaowu CSDN blog Client

Source: Internet
Author: User

Android Application Development-Get Comments list from xiaowu CSDN blog Client
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. 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 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; 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. setRefreshTime (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. setOnClickListener (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
 
  
{@ 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;} // get the comment List
  
   
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. 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 **/public class CommentAdapter extends BaseAdapter {private ViewHolder holder; private LayoutInflater layoutInflater; private Context context Context; private List
 
  
List; private SpannableStringBuilder htmlSpannable; private ImageLoader imageLoader = ImageLoader. getInstance (); private DisplayImageOptions options; private String replyText; public CommentAdapter (Context c) {super (); layoutInflater = (LayoutInflater) LayoutInflater. from (c); list = new ArrayList
  
   
(); 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 ). displayers (new FadeInBitmapDisplayer (300 )). build ();} public void setList (List
   
    
List) {this. list = list;} public void addList (List
    
     
List) {this. list. addAll (list);} public void clearList () {this. list. clear ();} public List
     
      
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 (p Osition); // get comments if (null = convertView) {holder = new ViewHolder (); switch (item. getType () {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 = (ImageView) 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 = (Tex TView) 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: // 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 is as follows: Download source code view: http://download.csdn.net/detail/wwj_748/7912513

Related Article

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.