HttpUtils请求XML加XListView刷新加载
2016-06-24 20:43
330 查看
package com.example.xmltest2; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.com.cctest.view.XListView; import org.com.cctest.view.XListView.IXListViewListener; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; 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.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Xml; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity implements IXListViewListener { String path = "http://www.oschina.net/action/api/news_list"; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { String xml = (String) msg.obj; xml(xml); xlistView.setAdapter(new MyAdapter(MainActivity.this, list)); for (News news : list) { System.out.println(news.toString()); } }; }; LinkedList<News> list = new LinkedList<>(); private News news; private XListView xlistView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xlistView = (XListView) findViewById(R.id.xlistView); //激活加载更多,默认不是激活的 xlistView.setPullLoadEnable(true); xlistView.setXListViewListener(this); getXml(); } private void getXml() { HttpUtils httpUtils = new HttpUtils(); httpUtils.configCurrentHttpCacheExpiry(0); httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub } @Override public void onSuccess(ResponseInfo<String> arg0) { String result = arg0.result; Message msg = Message.obtain(); msg.obj = result; handler.sendMessage(msg); } }); } /** * 解析xml * * @param xml */ protected void xml(String xml) { // 得到解析器 XmlPullParser pullParser = Xml.newPullParser(); // 把Stirng转成流 ByteArrayInputStream arrayInputStream = new ByteArrayInputStream( xml.getBytes()); try { pullParser.setInput(new InputStreamReader(arrayInputStream)); // 得到事件类型 int eventType = pullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: // 得到标签名 String tagName = pullParser.getName(); if ("news".equals(tagName)) { news = new News(); } else if ("title".equals(tagName)) { news.title = pullParser.nextText(); } else if ("pubDate".equals(tagName)) { news.pubDate = pullParser.nextText(); } break; case XmlPullParser.END_TAG: // 结束标签的名字 tagName = pullParser.getName(); if ("news".equals(tagName)) { list.addLast(news); } break; } eventType = pullParser.next(); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override//刷新 public void onRefresh() { getXml(); xlistView.stopLoadMore(); xlistView.stopRefresh(); } @Override//加载更多 public void onLoadMore() { getXml(); xlistView.stopLoadMore(); xlistView.stopRefresh(); } }
相关文章推荐
- 计算机网络体系结构详解(图文)
- 130行代码实现BP神经网络原理及应用举例
- iOS每日一记————————初识HTTP协议 Socket TCP/UDP
- 【JAVA】Socket网络编程
- Volley源码解析(三)网络请求流程
- [置顶] 台湾大学机器学习笔记——Neural Network 神经网络
- 对于httpclient上传文件的封装
- Python即时网络爬虫项目启动说明
- 数据包在用户空间的状态
- Spring mvc No mapping found for HTTP request with URI
- C++ STL 文件读取和容器(转载自http://blog.sina.com.cn/s/blog_735f29100102uwwd.html)
- Exception in thread "http-bio-8080-exec-2" java.lang.OutOfMemoryError: PermGen space
- [!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master Cloning
- 网络请求
- libubox组件(2)——blob/blobmsg (转载 https://segmentfault.com/a/1190000002391970)
- CDN网络访问原理
- http状态码
- (Android)使用httpclient上传文件到服务器
- iOS开发 - Swift实现检测网络连接状态及网络类型
- HttpClient请求DefaultHttpClient过时替换