httpUtils获取网络信息解析xml

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

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.ListView;
import android.widget.Toast;

import com.example.weektoexam.bean.News;
import com.example.weektoexam.bean.Oschina;
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;

public class MainActivity extends Activity {

    protected static final String TAG = "MainActivity";
    private Myadpter myadpter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listview = (ListView) findViewById(R.id.listview);
        myadpter = new Myadpter(this);
        listview.setAdapter(myadpter);
        resolve_xml();
    }

    private void resolve_xml() {
        // ����������
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.GET,
                "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=5&pageSize=20",
                new RequestCallBack<String>() {

                    private XmlPullParser newPullParser;
                    private Oschina oschina;
                    private News news;
                    private ArrayList<News> list;

                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            // ���ַ�ת����������
                            ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
                                    arg0.result.getBytes());
                            // xml������
                            newPullParser = Xml.newPullParser();
                            // ����
                            newPullParser.setInput(arrayInputStream, "utf-8");

                            list = new ArrayList<News>();

                            // �õ���ȡ��������
                            int eventType = newPullParser.getEventType();
                            while (eventType != XmlPullParser.END_DOCUMENT) {
                                String name = newPullParser.getName();
                                switch (eventType) {
                                case XmlPullParser.START_TAG:
                                    if ("news".equals(name)) {
                                        news = new News();
                                    } else if ("id".equals(name)) {
                                        news.id = newPullParser.nextText();
                                    } else if ("title".equals(name)) {
                                        news.title = newPullParser.nextText();
                                    } else if ("body".equals(name)) {
                                        news.body = newPullParser.nextText();
                                    } else if ("commentCount".equals(name)) {
                                        news.commentCount = newPullParser
                                                .nextText();
                                    } else if ("author".equals(name)) {
                                        news.author = newPullParser.nextText();
                                    } else if ("authorid".equals(name)) {
                                        news.authorid = newPullParser
                                                .nextText();
                                    } else if ("pubDate".equals(name)) {
                                        news.pubDate = newPullParser.nextText();
                                    } else if ("newstype".equals(name)) {
                                        if ("type".equals(name)) {
                                            news.pubDate = newPullParser
                                                    .nextText();
                                        } else if ("authoruid2".equals(name)) {
                                            news.pubDate = newPullParser
                                                    .nextText();
                                        }
                                    }
                                    break;    
                                case XmlPullParser.END_TAG:
                                    if ("news".equals(name)) {
                                        list.add(news);
                                    }
                                    myadpter.addList(list);

                                    break;
                                }
                                eventType = newPullParser.next();
                            }
                        } catch (Exception e) {
                        }

                    }
                });

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值