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.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";
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
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) {
e.printStackTrace();
}
}
@Override
public void onRefresh() {
pageIndex = 1;
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);
}