xListView(上拉刷新下拉加载,xml解析)中添加网络请求
2016-05-31 21:35
465 查看
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); } }
相关文章推荐
- 关于人工智能的幻想(九)人工智能与网络游戏第二部分
- 虚拟机网络类型选择
- #码神学习#第四天
- 记一下机器学习笔记 Rosenblatt感知机
- TCP定时器介绍
- 使用Jmeter进行http接口测试 ---------成都杀手
- 网络管理 -- TCP/IP协议簇
- Mac系统开发常见问题-80端口被占用的解决方案1-使用pfctl做网络层转发
- TCP的拥塞避免、超时重传、快速重传、快速恢复
- Linux 网络配置
- HttpUtils请求数据
- TCP 客户端 服务端详细代码
- HTTP消息头的讲解
- Kerberos(转:http://www.cnblogs.com/jankie/archive/2011/08/22/2149285.html)
- http
- HTML5+CSS3-第五节(边框背景图、多背景图、CSS选择器、网络字体、边框背景图片、过渡、放大、旋转)
- 在Android应用中使用自定义证书的HTTPS连接(下)
- Tcp/Ip通信协议
- 表单中 get与post提交方法的区别
- Java -网络IO编程