HttpUtils解析XMl

package com.example.yuekao1;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.example.adapter.MyAdapter;
import com.example.bean.Bean;
import com.example.view.XListView;
import com.example.view.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements IXListViewListener {

    private HttpUtils httpUtils;
    private XListView lv;
    private List<Bean> list;
    private String url = "http://www.oschina.net/action/api/tweet_list?pageIndex=";
    private int index = 0;
    private Bean bean;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        httpUtils = new HttpUtils();
        lv = (XListView) findViewById(R.id.lv);
        lv.setPullLoadEnable(true);
        lv.setXListViewListener(this);
        list = new ArrayList<Bean>();
        inData();
        iniData();
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent intent = new Intent(MainActivity.this,
                        InfoActivity.class);
                intent.putExtra("id", list.get(position - 1).id);
                startActivity(intent);
            }
        });
    }

    private void setAdapter() {
        if (adapter == null) {
            adapter = new MyAdapter(list, this);
            lv.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

    }

    private void inData() {
        httpUtils.send(HttpMethod.GET, url + (index++),
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(MainActivity.this, "网络请求失败", 0).show();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        String string = arg0.result;
                        ByteArrayInputStream inputStream = new ByteArrayInputStream(
                                string.getBytes());
                        XmlPullParser parser = Xml.newPullParser();
                        try {
                            parser.setInput(inputStream, "utf-8");
                            int type = parser.getEventType();
                            while (type != XmlPullParser.END_DOCUMENT) {
                                String name = parser.getName();
                                switch (type) {
                                case XmlPullParser.START_TAG:
                                    if ("tweet".equals(name)) {
                                        bean = new Bean();
                                    } else if ("id".equals(name)) {
                                        bean.id = Integer.parseInt(parser
                                                .nextText());
                                    } else if ("portrait".equals(name)) {
                                        bean.portrait = parser.nextText();
                                    } else if ("author".equals(name)) {
                                        bean.author = parser.nextText();
                                    } else if ("body".equals(name)) {
                                        bean.body = parser.nextText();
                                    } else if ("imgSmall".equals(name)) {
                                        bean.imgSmall = parser.nextText();
                                    }
                                    break;
                                case XmlPullParser.END_TAG:
                                    if ("tweet".equals(name)) {
                                        list.add(bean);
                                        bean = null;
                                    }
                                    break;
                                }
                                type = parser.next();
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        setAdapter();
                    }

                });

    }

    private void iniData() {
        httpUtils.send(HttpMethod.GET, url + (index++),
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(MainActivity.this, "网络请求失败", 0).show();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        String string = arg0.result;
                        ByteArrayInputStream inputStream = new ByteArrayInputStream(
                                string.getBytes());
                        XmlPullParser parser = Xml.newPullParser();
                        try {
                            parser.setInput(inputStream, "utf-8");
                            int type = parser.getEventType();
                            while (type != XmlPullParser.END_DOCUMENT) {
                                String name = parser.getName();
                                switch (type) {
                                case XmlPullParser.START_TAG:
                                    if ("tweet".equals(name)) {
                                        bean = new Bean();
                                    } else if ("id".equals(name)) {
                                        bean.id = Integer.parseInt(parser
                                                .nextText());
                                    } else if ("portrait".equals(name)) {
                                        bean.portrait = parser.nextText();
                                    } else if ("author".equals(name)) {
                                        bean.author = parser.nextText();
                                    } else if ("body".equals(name)) {
                                        bean.body = parser.nextText();
                                    } else if ("imgSmall".equals(name)) {
                                        bean.imgSmall = parser.nextText();
                                    }
                                    break;
                                case XmlPullParser.END_TAG:
                                    if ("tweet".equals(name)) {
                                        list.add(bean);
                                        bean = null;
                                    }
                                    break;
                                }
                                type = parser.next();
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        setAdapter();
                    }

                });

    }

    private void onLoad() {
        lv.stopRefresh();
        lv.stopLoadMore();
        lv.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {
        list.clear();
        inData();
        onLoad();

    }

    @Override
    public void onLoadMore() {
        inData();
        onLoad();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值