Android应用开发-小巫CSDN博客客户端之获取评论列表,kotlin爬虫

这篇博客展示了如何在Android应用中使用Kotlin实现获取CSDN博客评论列表的功能。通过HttpUtil发送请求,利用JsoupUtil解析JSON数据,然后使用CommentAdapter更新XListView。
摘要由CSDN通过智能技术生成

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

  • 博客评论列表

  • @author wwj_748

*/

public class BlogCommentActivity extends Activity implements

IXListViewRefreshListener, 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;

@Override

protected void onCreate(Bundle savedInstanceState) {

requestWindowFeature(Window.FEATURE_NO_TITLE);

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_comment);

init();

initComponent();

listView.setRefreshTime(DateUtil.getDate()); // 设置刷新时间

listView.startRefresh(); // 开始刷新

}

// 初始化

private void init() {

filename = getIntent().getExtras().getString(“filename”); // 获得文件名

page = new Page();

adapter = new CommentAdapter(this);

}

// 初始化组件

private void initComponent() {

progressBar = (ProgressBar) findViewById(R.id.newsContentPro);

reLoadImageView = (ImageView) findViewById(R.id.reLoadImage);

reLoadImageView.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

System.out.println(“click”);

reLoadImageView.setVisibility(View.INVISIBLE);

progressBar.setVisibility(View.VISIBLE);

new MainTask().execute(Constants.DEF_TASK_TYPE.REFRESH);

}

});

backBtn = (ImageView) findViewById(R.id.backBtn);

backBtn.setOnClickListener(new OnClickListener() {

@Override

public 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() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

}

});

}

@Override

public void finish() {

super.finish();

// 退出动画

overridePendingTransition(R.anim.push_no, R.anim.push_right_out);

}

private class MainTask extends AsyncTask<String, Void, Integer> {

@Override

protected Integer doInBackground(String… params) {

// 获得返回json字符串

String temp = HttpUtil.httpGet(URLUtil.getCommentListURL(filename,

page.getCurrentPage()));

if (temp == null) {

return Constants.DEF_RESULT_CODE.ERROR;

}

// 获得评论列表

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;

}

}

@Override

protected void onPostExecute(Integer result) {

if (result == Constants.DEF_RESULT_CODE.ERROR) {

Toast.makeText(getApplicationContext(), “网络信号不佳”,

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(), “无更多评论”,

Toast.LENGTH_SHORT).show();

listView.stopLoadMore();

listView.stopRefresh(DateUtil.getDate());

commentTV.setText(“共有评论:” + commentCount);

} else if (result == Constants.DEF_RESULT_CODE.LOAD) {

page.addPage();

pageIndex++;

adapter.notifyDataSetChanged();

listView.stopLoadMore();

} else if (result == Constants.DEF_RESULT_CODE.REFRESH) {

adapter.notifyDataSetChanged();

listView.stopRefresh(DateUtil.getDate());

page.setPage(2);

commentTV.setText(“共有评论:” + commentCount); // 显示评论数

}

progressBar.setVisibility(View.INVISIBLE);

super.onPostExecute(result);

}

}

// 加载更多

@Override

public void onLoadMore() {

new MainTask().execute(Constants.DEF_TASK_TYPE.LOAD);

}

// 刷新评论

@Override

public 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;

/**

  • 评论列表适配器

  • @author wwj_748

*/

public class CommentAdapter extends BaseAdapter {

private ViewHolder holder;

private LayoutInflater layoutInflater;

private 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©;

list = new ArrayList();

imageLoader.init(ImageLoaderConfiguration.createDefault©);

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(300)).build();

}

public void se

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

tList(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);

}

}

@Override

public int getCount() {

return list.size();

}

@Override

public Object getItem(int position) {

return list.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

Comment item = list.get(position); // 获取评论项

if (null == convertView) {

holder = new ViewHolder();

switch (item.getType()) {

case Constants.DEF_COMMENT_TYPE.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: // 子项

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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值