xListView(上拉刷新下拉加载,xml解析)中添加网络请求

来源:互联网 发布:excel批量插图软件 编辑:程序博客网 时间:2024/05/17 06:22
package com.example.week2;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.xmlpull.v1.XmlPullParser;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.util.Xml;import com.example.week2.adapter.MyAdapter;import com.example.week2.bean.News;import com.example.week2.xlist.SimpleDataExample;import com.example.week2.xlist.XListView;import com.example.week2.xlist.XListView.IXListViewListener;import com.koushikdutta.async.future.FutureCallback;import com.koushikdutta.ion.Ion;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
原创粉丝点击