HttpUtils请求XML加XListView刷新加载
来源:互联网 发布:店铺怎么加入农村淘宝 编辑:程序博客网 时间:2024/05/17 02:15
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(); }}
0 0
- HttpUtils请求XML加XListView刷新加载
- HttpUtils请求json加刷新加载
- HttpUtils请求网络+xlistview上拉刷新,下拉加载
- HttpUtils请求 和XlistView 实现下拉刷新和上拉加载
- pull解析请求网络的数据(带分页加载,刷新) xlistview HttpUtils
- httputils及xlistview刷新
- xListView(上拉刷新下拉加载,xml解析)中添加网络请求
- 网络请求xml,xListView加载更新
- httpUtils请求XML数据
- xml解析,httputils网络请求,imageloder图片加载
- XListView上拉刷新下拉加载(网络请求json)
- Xlistview加载刷新小案列
- XListView刷新、加载
- xlistview刷新 加载方法
- xml pull 解析 XlistView 上拉加载 下拉刷新
- 使用HttpUTILS请求XML数据
- XListView 刷新 + AsyncHttpClient网络请求
- XListView下拉刷新下拉加载
- C++引用与指针/值传递的比较
- MQ
- OpenSSH升级导致的SecureCRT无法登录
- php 获取gzip压缩过了的源码
- SQL Server性能调优系列
- HttpUtils请求XML加XListView刷新加载
- 计算机网络课程tcp复习笔记
- QLabel出现一会然后消失,起到提示作用
- Android Studio真机测试失败-----''No target device found"
- 笔试题81. LeetCode OJ (68)
- php 后期静态绑定 static 关键字的另一种用法
- [置顶] Android之Notification的多种用法
- 机器学习系列:(九)从感知器到支持向量机
- 基于SQL求集合的交、并、补