package com.lianxi.listViewjiazai;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import jiazai.SimpleDataExample;
import jiazai.XListView;
import jiazai.XListView.IXListViewListener;
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.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.lianxi.listViewjiazai.adapter.MyBase;
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.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity implements IXListViewListener{
private XListView listView;
private List<NewsList> nlist;
private NewsList nl;
private MyBase base;
List<NewsList> allList = new ArrayList<NewsList>();
private int pageIndex=1;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==1){
// InputStream input=(InputStream) msg.obj;
//allList.addAll(nlist);
//base.address(nlist);
// base.notifyDataSetChanged();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (XListView) findViewById(R.id.lv_main_listView);
base = new MyBase(MainActivity.this);
listView.setAdapter(base);
listView.setPullLoadEnable(true);
listView.setPullRefreshEnable(true);
listView.setXListViewListener(this);
getXmlData();
//shuQing();
}
private void getXmlData() {
HttpUtils httpUtils=new HttpUtils();
String url="http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="+pageIndex+"&pageSize=20";
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String result = arg0.result;
jieXi(result);
allList.addAll(nlist);
base.address(nlist);
System.out.println(nlist.size()+" oooooooooooooooooooooooooooooo");
}
});
}
//xml请求数据
// public void shuQing(){
//
//
// new Thread(){
//
// public void run() {
//
// try {
//
// String path="http://www.oschina.net/action/api/news_list?catalog=1&pageindex=1&pageSize=20";
//
// HttpClient client=new DefaultHttpClient();
// HttpGet get=new HttpGet(path);
// get.setHeader("Content-Type","text/xml");
// HttpResponse httpResponse = client.execute(get);
//
// int statusCode = httpResponse.getStatusLine().getStatusCode();
//
// if(statusCode==200){
//
//
// InputStream inputStream = httpResponse.getEntity().getContent();
//
// streamTo(inputStream);
//
// }
//
// } catch (IllegalStateException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
//
// };
//
//
// }.start();
//}
//xml解析
public void jieXi(String str){
try {
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(str.getBytes());
//获取PullPger解析器
XmlPullParser parser=Xml.newPullParser();
//设置解析的流,并设置编码格式
parser.setInput(byteArrayInputStream, "utf-8");
//事件类型,包括: 文件开始标签,开始标签,结束标签,文档结束标签
int eventType = parser.getEventType();
//循环,如果不是文件结束标签 document文件
while(eventType!=XmlPullParser.END_DOCUMENT){
switch(eventType){
case XmlPullParser.START_DOCUMENT:
nlist = new ArrayList<NewsList>();
break;
case XmlPullParser.START_TAG:
//判断是否是开始标签
if("news".equals(parser.getName())){
nl = new NewsList();
}
if(nl!=null){
if ("id".equals(parser.getName())) {
nl.id = parser.nextText();
} else if ("title".equals(parser.getName())) {
System.out.println("tttt=="+parser.getName());
nl.title = parser.nextText();
} else if ("body".equals(parser.getName())) {
nl.body = parser.nextText();
} else if ("commentCount".equals(parser.getName())) {
nl.commentCount = parser.nextText();
} else if ("author".equals(parser.getName())) {
nl.author = parser.nextText();
} else if ("authorid".equals(parser.getName())) {
nl.authorid = parser.nextText();
} else if ("pubDate".equals(parser.getName())) {
nl.pubDate = parser.nextText();
} else if ("newstype".equals(parser.getName())) {
if ("type".equals(parser.getName())) {
nl.newstype.type = parser.nextText();
} else if ("authoruid2".equals(parser.getName())) {
nl.newstype.authoruid2 = parser.nextText();
}
}
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_TAG:
// 判断是不是 结束标签
if ("news".equals(parser.getName())) {
nlist.add(nl);
}
break;
}
//继续执行
eventType=parser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
pageIndex=1;
nlist.clear();
allList.clear();
getXmlData();
SimpleDataExample.setFormat("DDD", MainActivity.this);
SimpleDataExample.getFormat("DDD", MainActivity.this, listView);
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
pageIndex++;
nlist.clear();
System.out.println("pageIndex====="+pageIndex);
getXmlData();
}
//输出流
// public void streamTo(InputStream input){
//
// ByteArrayOutputStream baos=new ByteArrayOutputStream();
// byte[] by=new byte[1024];
// int len=0;
// try {
// while((len=input.read(by))!=-1){
// baos.write(by, 0, len);
// }
// System.out.println("baos=="+baos.toString());
// System.out.println( baos.toString());
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
//
// }
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import jiazai.SimpleDataExample;
import jiazai.XListView;
import jiazai.XListView.IXListViewListener;
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.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.lianxi.listViewjiazai.adapter.MyBase;
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.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity implements IXListViewListener{
private XListView listView;
private List<NewsList> nlist;
private NewsList nl;
private MyBase base;
List<NewsList> allList = new ArrayList<NewsList>();
private int pageIndex=1;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==1){
// InputStream input=(InputStream) msg.obj;
//allList.addAll(nlist);
//base.address(nlist);
// base.notifyDataSetChanged();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (XListView) findViewById(R.id.lv_main_listView);
base = new MyBase(MainActivity.this);
listView.setAdapter(base);
listView.setPullLoadEnable(true);
listView.setPullRefreshEnable(true);
listView.setXListViewListener(this);
getXmlData();
//shuQing();
}
private void getXmlData() {
HttpUtils httpUtils=new HttpUtils();
String url="http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="+pageIndex+"&pageSize=20";
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String result = arg0.result;
jieXi(result);
allList.addAll(nlist);
base.address(nlist);
System.out.println(nlist.size()+" oooooooooooooooooooooooooooooo");
}
});
}
//xml请求数据
// public void shuQing(){
//
//
// new Thread(){
//
// public void run() {
//
// try {
//
// String path="http://www.oschina.net/action/api/news_list?catalog=1&pageindex=1&pageSize=20";
//
// HttpClient client=new DefaultHttpClient();
// HttpGet get=new HttpGet(path);
// get.setHeader("Content-Type","text/xml");
// HttpResponse httpResponse = client.execute(get);
//
// int statusCode = httpResponse.getStatusLine().getStatusCode();
//
// if(statusCode==200){
//
//
// InputStream inputStream = httpResponse.getEntity().getContent();
//
// streamTo(inputStream);
//
// }
//
// } catch (IllegalStateException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
//
// };
//
//
// }.start();
//}
//xml解析
public void jieXi(String str){
try {
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(str.getBytes());
//获取PullPger解析器
XmlPullParser parser=Xml.newPullParser();
//设置解析的流,并设置编码格式
parser.setInput(byteArrayInputStream, "utf-8");
//事件类型,包括: 文件开始标签,开始标签,结束标签,文档结束标签
int eventType = parser.getEventType();
//循环,如果不是文件结束标签 document文件
while(eventType!=XmlPullParser.END_DOCUMENT){
switch(eventType){
case XmlPullParser.START_DOCUMENT:
nlist = new ArrayList<NewsList>();
break;
case XmlPullParser.START_TAG:
//判断是否是开始标签
if("news".equals(parser.getName())){
nl = new NewsList();
}
if(nl!=null){
if ("id".equals(parser.getName())) {
nl.id = parser.nextText();
} else if ("title".equals(parser.getName())) {
System.out.println("tttt=="+parser.getName());
nl.title = parser.nextText();
} else if ("body".equals(parser.getName())) {
nl.body = parser.nextText();
} else if ("commentCount".equals(parser.getName())) {
nl.commentCount = parser.nextText();
} else if ("author".equals(parser.getName())) {
nl.author = parser.nextText();
} else if ("authorid".equals(parser.getName())) {
nl.authorid = parser.nextText();
} else if ("pubDate".equals(parser.getName())) {
nl.pubDate = parser.nextText();
} else if ("newstype".equals(parser.getName())) {
if ("type".equals(parser.getName())) {
nl.newstype.type = parser.nextText();
} else if ("authoruid2".equals(parser.getName())) {
nl.newstype.authoruid2 = parser.nextText();
}
}
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_TAG:
// 判断是不是 结束标签
if ("news".equals(parser.getName())) {
nlist.add(nl);
}
break;
}
//继续执行
eventType=parser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
pageIndex=1;
nlist.clear();
allList.clear();
getXmlData();
SimpleDataExample.setFormat("DDD", MainActivity.this);
SimpleDataExample.getFormat("DDD", MainActivity.this, listView);
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
pageIndex++;
nlist.clear();
System.out.println("pageIndex====="+pageIndex);
getXmlData();
}
//输出流
// public void streamTo(InputStream input){
//
// ByteArrayOutputStream baos=new ByteArrayOutputStream();
// byte[] by=new byte[1024];
// int len=0;
// try {
// while((len=input.read(by))!=-1){
// baos.write(by, 0, len);
// }
// System.out.println("baos=="+baos.toString());
// System.out.println( baos.toString());
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
//
// }
}