Android仿网易客户端新闻案例

来源:互联网 发布:mac照片导出到移动硬盘 编辑:程序博客网 时间:2024/05/22 07:51

学到知识:

1.消息处理机制:Handler

2.JavaBean结合集合运用

3.解析服务器端xml的内容

4.Adapter适配


Activity

import com.itheima28.neteasedemo.domain.NewInfo;import com.loopj.android.image.SmartImageView;public class MainActivity extends Activity {    private static final String TAG = "MainActivity";    private final int SUCCESS = 0;    private final int FAILED = 1;private ListView lvNews;private List<NewInfo> newInfoList;    private Handler handler = new Handler() {/**     * 接收消息     */@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case SUCCESS:// 访问成功, 有数据// 给Listview列表绑定数据newInfoList = (List<NewInfo>) msg.obj;MyAdapter adapter = new MyAdapter();lvNews.setAdapter(adapter);break;case FAILED:// 无数据Toast.makeText(MainActivity.this, "当前网络崩溃了.", 0).show();break;default:break;}}    };@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }private void init() {lvNews = (ListView) findViewById(R.id.lv_news);// 抓取新闻数据new Thread(new Runnable() {@Overridepublic void run() {// 获得新闻集合List<NewInfo> newInfoList = getNewsFromInternet();Message msg = new Message();if(newInfoList != null) {msg.what = SUCCESS;msg.obj = newInfoList;} else {msg.what = FAILED;}handler.sendMessage(msg);}}).start();}/** * 返回新闻信息 */private List<NewInfo> getNewsFromInternet() {HttpClient client = null;try {// 定义一个客户端client = new DefaultHttpClient();// 定义get方法HttpGet get = new HttpGet("http://10.0.2.2:8081/NetEaseServer/new.xml");// 执行请求HttpResponse response = client.execute(get);int statusCode = response.getStatusLine().getStatusCode();if(statusCode == 200) {InputStream is = response.getEntity().getContent();List<NewInfo> newInfoList = getNewListFromInputStream(is);return newInfoList;} else {Log.i(TAG, "访问失败: " + statusCode);}} catch (Exception e) {e.printStackTrace();} finally {if(client != null) {client.getConnectionManager().shutdown();// 关闭和释放资源}}return null;}    /** * 从流中解析新闻集合 * @param is * @return */private List<NewInfo> getNewListFromInputStream(InputStream is) throws Exception {XmlPullParser parser = Xml.newPullParser();// 创建一个pull解析器parser.setInput(is, "utf-8");// 指定解析流, 和编码int eventType = parser.getEventType();List<NewInfo> newInfoList = null;NewInfo newInfo = null;while(eventType != XmlPullParser.END_DOCUMENT) {// 如果没有到结尾处, 继续循环String tagName = parser.getName();// 节点名称switch (eventType) {case XmlPullParser.START_TAG: // <news>if("news".equals(tagName)) {newInfoList = new ArrayList<NewInfo>();} else if("new".equals(tagName)) {newInfo = new NewInfo();} else if("title".equals(tagName)) {newInfo.setTitle(parser.nextText());} else if("detail".equals(tagName)) {newInfo.setDetail(parser.nextText());} else if("comment".equals(tagName)) {newInfo.setComment(Integer.valueOf(parser.nextText()));} else if("image".equals(tagName)) {newInfo.setImageUrl(parser.nextText());}break;case XmlPullParser.END_TAG:        //结果的节点if("new".equals(tagName)) {newInfoList.add(newInfo);}break;default:break;}eventType = parser.next();// 取下一个事件类型}return newInfoList;}class MyAdapter extends BaseAdapter {/** * 返回列表的总长度 */@Overridepublic int getCount() {return newInfoList.size();}/** * 返回一个列表的子条目的布局 */@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = null;if(convertView == null) {LayoutInflater inflater = getLayoutInflater();view = inflater.inflate(R.layout.listview_item, null);} else {view = convertView;}// 重新赋值, 不会产生缓存对象中原有数据保留的现象SmartImageView sivIcon = (SmartImageView) view.findViewById(R.id.siv_listview_item_icon);TextView tvTitle = (TextView) view.findViewById(R.id.tv_listview_item_title);TextView tvDetail = (TextView) view.findViewById(R.id.tv_listview_item_detail);TextView tvComment = (TextView) view.findViewById(R.id.tv_listview_item_comment);NewInfo newInfo = newInfoList.get(position);sivIcon.setImageUrl(newInfo.getImageUrl());// 设置图片tvTitle.setText(newInfo.getTitle());tvDetail.setText(newInfo.getDetail());tvComment.setText(newInfo.getComment() + "跟帖");return view;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}}}



JavaBean类

/** * @author andong * 新闻信息实体类 */public class NewInfo {private String title; // 标题private String detail; // 详细private Integer comment; // 跟帖数量private String imageUrl; // 图片连接@Overridepublic String toString() {return "NewInfo [title=" + title + ", detail=" + detail + ", comment="+ comment + ", imageUrl=" + imageUrl + "]";}public NewInfo(String title, String detail, Integer comment, String imageUrl) {super();this.title = title;this.detail = detail;this.comment = comment;this.imageUrl = imageUrl;}public NewInfo() {super();// TODO Auto-generated constructor stub}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;}public Integer getComment() {return comment;}public void setComment(Integer comment) {this.comment = comment;}public String getImageUrl() {return imageUrl;}public void setImageUrl(String imageUrl) {this.imageUrl = imageUrl;}}


其中适配的xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:padding="5dip" >    <com.loopj.android.image.SmartImageView        android:id="@+id/siv_listview_item_icon"        android:layout_width="100dip"        android:layout_height="60dip"        android:src="@drawable/a" />    <TextView        android:id="@+id/tv_listview_item_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="3dip"        android:layout_toRightOf="@id/siv_listview_item_icon"        android:singleLine="true"        android:text="3Q大战宣判: 腾讯获赔500万"        android:textColor="@android:color/black"        android:textSize="17sp" />    <TextView        android:id="@+id/tv_listview_item_detail"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@id/tv_listview_item_title"        android:layout_below="@id/tv_listview_item_title"        android:layout_marginTop="3dip"        android:text="啊发送旅客登机挥发速度发送旅客登机"        android:textColor="@android:color/darker_gray"        android:textSize="14sp" />    <TextView        android:id="@+id/tv_listview_item_comment"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="668跟帖"        android:textColor="#FF0000"        android:textSize="12sp" /></RelativeLayout>


服务器端xml

<?xml version="1.0" encoding="UTF-8" ?><news><new><title>3Q大战宣判: 腾讯获赔500万</title><detail>最高法驳回360上诉, 维持一审宣判.</detail><comment>6427</comment><image>http://10.0.2.2:8080/NetEaseServer/images/1.jpg</image></new><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail><comment>681</comment><image>http://10.0.2.2:8080/NetEaseServer/images/2.jpg</image></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail><comment>1359</comment><image>http://10.0.2.2:8080/NetEaseServer/images/3.jpg</image></new><new><title>轻松一刻: 我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail><comment>10116</comment><image>http://10.0.2.2:8080/NetEaseServer/images/4.jpg</image></new><new><title>男女那些事儿</title><detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail><comment>10339</comment><image>http://10.0.2.2:8080/NetEaseServer/images/5.jpg</image></new><new><title>3Q大战宣判: 腾讯获赔500万</title><detail>最高法驳回360上诉, 维持一审宣判.</detail><comment>6427</comment><image>http://10.0.2.2:8080/NetEaseServer/images/1.jpg</image></new><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail><comment>681</comment><image>http://10.0.2.2:8080/NetEaseServer/images/2.jpg</image></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail><comment>1359</comment><image>http://10.0.2.2:8080/NetEaseServer/images/3.jpg</image></new><new><title>轻松一刻: 我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail><comment>10116</comment><image>http://10.0.2.2:8080/NetEaseServer/images/4.jpg</image></new><new><title>男女那些事儿</title><detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail><comment>10339</comment><image>http://10.0.2.2:8080/NetEaseServer/images/5.jpg</image></new><new><title>3Q大战宣判: 腾讯获赔500万</title><detail>最高法驳回360上诉, 维持一审宣判.</detail><comment>6427</comment><image>http://10.0.2.2:8080/NetEaseServer/images/1.jpg</image></new><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail><comment>681</comment><image>http://10.0.2.2:8080/NetEaseServer/images/2.jpg</image></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail><comment>1359</comment><image>http://10.0.2.2:8080/NetEaseServer/images/3.jpg</image></new><new><title>轻松一刻: 我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail><comment>10116</comment><image>http://10.0.2.2:8080/NetEaseServer/images/4.jpg</image></new><new><title>男女那些事儿</title><detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail><comment>10339</comment><image>http://10.0.2.2:8080/NetEaseServer/images/5.jpg</image></new></news>

项目运行图:





项目代码下载:http://download.csdn.net/detail/itjavawfc/8139623














0 0
原创粉丝点击