package com.example.yuekao1;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.example.adapter.MyAdapter;
import com.example.bean.Bean;
import com.example.view.XListView;
import com.example.view.XListView.IXListViewListener;
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.app.Activity;
import android.content.Intent;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements IXListViewListener {
private HttpUtils httpUtils;
private XListView lv;
private List<Bean> list;
private String url = "http://www.oschina.net/action/api/tweet_list?pageIndex=";
private int index = 0;
private Bean bean;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpUtils = new HttpUtils();
lv = (XListView) findViewById(R.id.lv);
lv.setPullLoadEnable(true);
lv.setXListViewListener(this);
list = new ArrayList<Bean>();
inData();
iniData();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this,
InfoActivity.class);
intent.putExtra("id", list.get(position - 1).id);
startActivity(intent);
}
});
}
private void setAdapter() {
if (adapter == null) {
adapter = new MyAdapter(list, this);
lv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
private void inData() {
httpUtils.send(HttpMethod.GET, url + (index++),
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(MainActivity.this, "网络请求失败", 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String string = arg0.result;
ByteArrayInputStream inputStream = new ByteArrayInputStream(
string.getBytes());
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(inputStream, "utf-8");
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
switch (type) {
case XmlPullParser.START_TAG:
if ("tweet".equals(name)) {
bean = new Bean();
} else if ("id".equals(name)) {
bean.id = Integer.parseInt(parser
.nextText());
} else if ("portrait".equals(name)) {
bean.portrait = parser.nextText();
} else if ("author".equals(name)) {
bean.author = parser.nextText();
} else if ("body".equals(name)) {
bean.body = parser.nextText();
} else if ("imgSmall".equals(name)) {
bean.imgSmall = parser.nextText();
}
break;
case XmlPullParser.END_TAG:
if ("tweet".equals(name)) {
list.add(bean);
bean = null;
}
break;
}
type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
setAdapter();
}
});
}
private void iniData() {
httpUtils.send(HttpMethod.GET, url + (index++),
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(MainActivity.this, "网络请求失败", 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String string = arg0.result;
ByteArrayInputStream inputStream = new ByteArrayInputStream(
string.getBytes());
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(inputStream, "utf-8");
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
switch (type) {
case XmlPullParser.START_TAG:
if ("tweet".equals(name)) {
bean = new Bean();
} else if ("id".equals(name)) {
bean.id = Integer.parseInt(parser
.nextText());
} else if ("portrait".equals(name)) {
bean.portrait = parser.nextText();
} else if ("author".equals(name)) {
bean.author = parser.nextText();
} else if ("body".equals(name)) {
bean.body = parser.nextText();
} else if ("imgSmall".equals(name)) {
bean.imgSmall = parser.nextText();
}
break;
case XmlPullParser.END_TAG:
if ("tweet".equals(name)) {
list.add(bean);
bean = null;
}
break;
}
type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
setAdapter();
}
});
}
private void onLoad() {
lv.stopRefresh();
lv.stopLoadMore();
lv.setRefreshTime("刚刚");
}
@Override
public void onRefresh() {
list.clear();
inData();
onLoad();
}
@Override
public void onLoadMore() {
inData();
onLoad();
}
}
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.example.adapter.MyAdapter;
import com.example.bean.Bean;
import com.example.view.XListView;
import com.example.view.XListView.IXListViewListener;
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.app.Activity;
import android.content.Intent;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements IXListViewListener {
private HttpUtils httpUtils;
private XListView lv;
private List<Bean> list;
private String url = "http://www.oschina.net/action/api/tweet_list?pageIndex=";
private int index = 0;
private Bean bean;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpUtils = new HttpUtils();
lv = (XListView) findViewById(R.id.lv);
lv.setPullLoadEnable(true);
lv.setXListViewListener(this);
list = new ArrayList<Bean>();
inData();
iniData();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this,
InfoActivity.class);
intent.putExtra("id", list.get(position - 1).id);
startActivity(intent);
}
});
}
private void setAdapter() {
if (adapter == null) {
adapter = new MyAdapter(list, this);
lv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
private void inData() {
httpUtils.send(HttpMethod.GET, url + (index++),
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(MainActivity.this, "网络请求失败", 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String string = arg0.result;
ByteArrayInputStream inputStream = new ByteArrayInputStream(
string.getBytes());
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(inputStream, "utf-8");
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
switch (type) {
case XmlPullParser.START_TAG:
if ("tweet".equals(name)) {
bean = new Bean();
} else if ("id".equals(name)) {
bean.id = Integer.parseInt(parser
.nextText());
} else if ("portrait".equals(name)) {
bean.portrait = parser.nextText();
} else if ("author".equals(name)) {
bean.author = parser.nextText();
} else if ("body".equals(name)) {
bean.body = parser.nextText();
} else if ("imgSmall".equals(name)) {
bean.imgSmall = parser.nextText();
}
break;
case XmlPullParser.END_TAG:
if ("tweet".equals(name)) {
list.add(bean);
bean = null;
}
break;
}
type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
setAdapter();
}
});
}
private void iniData() {
httpUtils.send(HttpMethod.GET, url + (index++),
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(MainActivity.this, "网络请求失败", 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String string = arg0.result;
ByteArrayInputStream inputStream = new ByteArrayInputStream(
string.getBytes());
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(inputStream, "utf-8");
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
switch (type) {
case XmlPullParser.START_TAG:
if ("tweet".equals(name)) {
bean = new Bean();
} else if ("id".equals(name)) {
bean.id = Integer.parseInt(parser
.nextText());
} else if ("portrait".equals(name)) {
bean.portrait = parser.nextText();
} else if ("author".equals(name)) {
bean.author = parser.nextText();
} else if ("body".equals(name)) {
bean.body = parser.nextText();
} else if ("imgSmall".equals(name)) {
bean.imgSmall = parser.nextText();
}
break;
case XmlPullParser.END_TAG:
if ("tweet".equals(name)) {
list.add(bean);
bean = null;
}
break;
}
type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
setAdapter();
}
});
}
private void onLoad() {
lv.stopRefresh();
lv.stopLoadMore();
lv.setRefreshTime("刚刚");
}
@Override
public void onRefresh() {
list.clear();
inData();
onLoad();
}
@Override
public void onLoadMore() {
inData();
onLoad();
}
}