android(23)(网易新闻的简单例子:包括pull解析,httpclient和ListView等知识点)

1.布局文件:ListView布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
ListView_item的布局文件:
<?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>
2.业务逻辑代码的实现:
(1)实体类:

/** * 新闻信息实体类 */
public class NewInfo {

    private String title; // 标题
    private String detail; // 详细
    private Integer comment; // 跟帖数量
    private String imageUrl; // 图片连接
    @Override
    public 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;
    }
}
(2)业务逻辑类:
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() {

        /** * 接收消息 */
        @Override
        public 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() {
            @Override
            public 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://192.168.1.254:8080/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: // </news>
                if("new".equals(tagName)) {
                    newInfoList.add(newInfo);
                }
                break;
            default:
                break;
            }
            eventType = parser.next();      // 取下一个事件类型
        }
        return newInfoList;
    }
    //适配器
    class MyAdapter extends BaseAdapter {

        /** * 返回列表的总长度 */
        @Override
        public int getCount() {
            return newInfoList.size();
        }

        /** * 返回一个列表的子条目的布局 */
        @Override
        public 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;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    }

你可能感兴趣的:(ListView,新闻)