关闭

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

297人阅读 评论(0) 收藏 举报
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
查看评论

XListView上拉刷新下拉加载(网络请求json)

XML布局activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and...
  • xuanming0215
  • xuanming0215
  • 2016-06-01 11:35
  • 389

XListView上拉刷新,下拉加载--pizifusheng

 导入XListView工具类:{File>ImportModule...>导入工具类地址》》》》导完之后右键需要用到工具类的工程>Open Module Settings>Dependencies>工具类>确定OK}加载权限: 布局:<com.ba...
  • pizifusheng
  • pizifusheng
  • 2017-12-27 11:49
  • 14

XListView实现上拉刷新下拉加载

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {     private XListView lv;   &#...
  • lisiruxiaoniu
  • lisiruxiaoniu
  • 2017-07-25 09:02
  • 189

Xlistview上拉刷新和下拉加载

package lianxi.bawei.com.day10_xlistview_lianxi; import android.os.AsyncTask; import android.support.v4.widget.DrawerLayout; import android.sup...
  • dxt0528
  • dxt0528
  • 2017-12-27 08:48
  • 48

xlistview上拉刷新和下拉加载

package com.example.a09_xlistview_demo; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; impo...
  • tj45678
  • tj45678
  • 2017-10-11 18:23
  • 40

XListView上拉刷新,下拉加载

先写三个类,分别是自定义的XListView,XListViewHeader,XListViewFooter 1.XListView/** * @file XListView.java * @package me.maxwin.view * @create Mar 18, 2012 6:28...
  • Geri_Yang
  • Geri_Yang
  • 2016-05-25 14:39
  • 176

XlistView的上拉刷新————下拉加载

首先导入三个类 //第一个类 /** * @file XListView.java * @package me.maxwin.view * @create Mar 18, 2012 6:28:41 PM * @author Maxwin * @description An ListVi...
  • Zhangshiting
  • Zhangshiting
  • 2016-06-24 16:24
  • 1979

xlistview上拉刷新,下拉加载

package com.bwei.yuekao;import java.io.ByteArrayInputStream; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayL...
  • qq_35384557
  • qq_35384557
  • 2016-07-01 08:08
  • 115

xlistview 上拉刷新 下拉加载

//判断是否联网 //切记加权限 uses-permission android:name="android.permission.INTERNET">uses-permission> uses-permission android:name="andr...
  • liu461211527
  • liu461211527
  • 2017-08-18 19:10
  • 90

xListView上拉刷新下拉加载

xlistview一共分3部分XListViewHeader头部、XListViewFooter尾部、XListView主体 一、XListViewHeader头部 先是XML <LinearLayout xmlns:android="http://schemas.andro...
  • qq_32659073
  • qq_32659073
  • 2015-12-27 11:42
  • 164
    个人资料
    • 访问:10284次
    • 积分:432
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:4篇
    • 译文:1篇
    • 评论:0条