xml解析

public class MainActivity extends Activity implements IXListViewListener {

    protected static final String tag = "MainActivity";
    int pageIndex = 1;
    private String path;
    private List<News> al;
    private News n;
    private MyAdapter adapter;
    private XListView xlistView;

    List<News> all = new ArrayList<News>();

    Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {

        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xlistView = (XListView) findViewById(R.id.listView);
        adapter = new MyAdapter(MainActivity.this);
        xlistView.setAdapter(adapter);

        // 添加XListView的上拉和下拉刷新监听器
        xlistView.setPullLoadEnable(true);
        xlistView.setPullRefreshEnable(true);
        xlistView.setXListViewListener(this);
        // 网络请求
        getData();

    }

    private void getData() {
        path = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="
                + pageIndex + "&pageSize=20";
        /*// ion jar包用于网络请求
        Ion.with(getApplicationContext()).load(path).asString()
                .setCallback(new FutureCallback<String>() {

                    @Override
                    public void onCompleted(Exception arg0, String result) {
                        // TODO Auto-generated method stub
                        if (arg0 != null) {
                            return;
                        }

                        pull(result);
                        // Log.i(tag, result);

                        // 要重新定义一个集合all,把解析出的集合al填入all中,不然会出现上拉加载时加载出来的数据把原集合覆盖diao
                        all.addAll(al);
                        adapter.addrest(all);
                    }
                });*/
        new Thread(){
            public void run() {

                try {
                    HttpClient client=new DefaultHttpClient();
                    HttpGet httpGet=new HttpGet(path);
                    HttpResponse httpResponse = client.execute(httpGet);
                    int statusCode = httpResponse.getStatusLine().getStatusCode();
                    Log.i(tag, statusCode+"=========");
                    if(statusCode==200){
                        InputStream inputStream = httpResponse.getEntity().getContent();
                        Log.i(tag, inputStream.toString()+"=========");
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            };
        }.start();

    }

    // 解析XML
    public void pull(String xml) {
        try {
            XmlPullParser newPullParser = Xml.newPullParser();
            ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
                    xml.getBytes());
            // 设置要解析的内容
            newPullParser.setInput(arrayInputStream, "utf-8");
            // 获取解析的事件类型
            int eventType = newPullParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                // 获得标签的名字
                String name = newPullParser.getName();

                switch (eventType) {
                // 开始接受文件(走一次)
                case XmlPullParser.START_DOCUMENT:
                    al = new ArrayList<News>();
                    break;
                case XmlPullParser.START_TAG:
                    if ("news".equals(name)) {
                        n = new News();
                    } else if ("id".equals(name)) {
                        String id = newPullParser.nextText();
                        n.id = id;
                    } else if ("title".equals(name)) {
                        String title = newPullParser.nextText();
                        n.title = title;
                    } else if ("body".equals(name)) {
                        String body = newPullParser.nextText();
                        n.body = body;
                    } else if ("commentCount".equals(name)) {
                        String commentCount = newPullParser.nextText();
                        n.commentCount = commentCount;
                    } else if ("author".equals(name)) {
                        String author = newPullParser.nextText();
                        n.author = author;
                    } else if ("authorid".equals(name)) {
                        String authorid = newPullParser.nextText();
                        n.authorid = authorid;
                    } else if ("pubDate".equals(name)) {
                        String pubDate = newPullParser.nextText();
                        n.pubDate = pubDate;
                    } else if ("newstype".equals(name)) {
                        if ("type".equals(name)) {
                            String type = newPullParser.nextText();
                            n.newstype.type = type;
                        } else if ("authoruid2".equals(name)) {
                            String authoruid2 = newPullParser.nextText();
                            n.newstype.authoruid2 = authoruid2;
                        }
                    }
                    break;

                case XmlPullParser.END_TAG:
                    if ("news".equals(name)) {
                        al.add(n);

                    }
                    break;

                }
                eventType = newPullParser.next();
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        // 刷新的时候自动显示第一页的信息,并把加载出来的信息不显示, 就需要把getData() all.clear();这两个方法位置调换
        getData();
        all.clear();

        SimpleDataExample.setFormat("dddddddddddd", getApplicationContext());
        SimpleDataExample.getFormat("dddddddddddd", getApplicationContext(),
                xlistView);

    }

    @Override
    public void onLoadMore() {
        pageIndex++;
        getData();

        SimpleDataExample.setFormat("dddddddddddd", getApplicationContext());
        SimpleDataExample.getFormat("dddddddddddd", getApplicationContext(),
                xlistView);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值