MainActivity.java
public class MainActivity extends Activity {
private ListView lv;
private ArrayList<NewsList> ilist;
private Madapter adapter;
private ProgressDialog d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ilist=new ArrayList<NewsList>();
lv = (ListView) findViewById(R.id.l_lv);
MyAsyncTask at=new MyAsyncTask();
at.execute("http://www.oschina.net/action/api/news_list");
}
class MyAsyncTask extends AsyncTask<String, Void,ArrayList<NewsList>>{
@Override
protected ArrayList<NewsList> doInBackground(String... params) {
// TODO Auto-generated method stub
return getData(params[0]);
}
@Override
protected void onPreExecute() {
d = new ProgressDialog(MainActivity.this);
d.setTitle("正在加载");
d.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(final ArrayList<NewsList> result) {
// TODO Auto-generated method stub
d.dismiss();
ilist.addAll(result);
if(adapter==null){
adapter = new Madapter(MainActivity.this,ilist);
lv.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(ilist.size()-1 ==view.getLastVisiblePosition()&& scrollState==AbsListView.OnScrollListener.SCROLL_STATE_IDLE ){
MyAsyncTask at=new MyAsyncTask();
at.execute("http://www.oschina.net/action/api/news_list");
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public ArrayList<NewsList> getData(String path) {
// TODO Auto-generated method stub
ArrayList<NewsList> list=new ArrayList<NewsList>();
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setReadTimeout(6000);
conn.setConnectTimeout(6000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inputStream2 = conn.getInputStream();
list = Pullutile.getData(inputStream2);
System.out.println(list.get(0).getTitle());
}
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list ;
}
}
pull解析
Pullutile.java
public class Pullutile {
private static NewsList n;
public static ArrayList<NewsList> getData(InputStream inputStream){
ArrayList<NewsList> list=new ArrayList<NewsList>();
XmlPullParser parser=Xml.newPullParser();
try {
parser.setInput(inputStream, "utf-8");
int eventType = parser.getEventType();
while(eventType!=parser.END_DOCUMENT){
String name = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if("news".equals(name)){
n = new NewsList();
}else if("id".equals(name)){
n.setId(parser.nextText() );
}else if("title".equals(name)){
n.setTitle(parser.nextText() );
}
break;
case XmlPullParser.END_TAG:
if("news".equals(name)){
list.add(n);
n=null;
}
}
//这句必须加,让指针指向下一个xml
eventType=parser.next();
}
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
// 得到person元素的第一个属性,也就是ID
n.setId(Integer.parseInt(parser.getAttributeValue(0)));