==============================main
package com.example.xmlpull;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
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.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private String path = "http://www.oschina.net/action/api/news_list?http://www.oschina.net/action/api/news_list=1&http://www.oschina.net/action/api/news_list=1&pageSize=20";
private ListView listView;
private int type;
private Bean bean2;
private InputStream inputStream;
private XmlPullParser parser;
private List<Bean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.main_list);
initData();
}
private void initData() {
// Xutile网络请求
HttpUtils httpUtils = new HttpUtils();
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 xmlString = arg0.result;
// 转换输入流
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(
xmlString.getBytes());
list = getxml(tInputStringStream);
listView.setAdapter(new MyAdapter());
}
private List<Bean> getxml(InputStream is) {
List<Bean> arrayList = null;
try {
parser = Xml.newPullParser();
//1.
parser.setInput(is, "utf-8");
type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
switch (type) {
case XmlPullParser.START_DOCUMENT:
arrayList = new ArrayList<Bean>();
break;
case XmlPullParser.START_TAG:
if (name.equals("news")) {
bean2 = new Bean();
} else if (name.equals("id")) {
bean2.setId(parser.nextText());
} else if (name.equals("title")) {
bean2.setTitle(parser.nextText());
} else if (name.equals("body")) {
bean2.setBody(parser.nextText());
} else if (name.equals("commentCount")) {
bean2.setCommentCount(parser.nextText());
} else if (name.equals("author")) {
bean2.setAuthor(parser.nextText());
} else if (name.equals("authoruid2")) {
bean2.setAuthoruid2(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if (name.equals("news")) {
arrayList.add(bean2);
bean2 = null;
}
break;
default:
break;
}
type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return arrayList;
}
});
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView = View.inflate(getApplicationContext(), R.layout.item,null);
}
TextView tv1 = (TextView) convertView.findViewById(R.id.textView1);
TextView tv2 = (TextView) convertView.findViewById(R.id.textView2);
TextView tv3 = (TextView) convertView.findViewById(R.id.textView3);
TextView tv4 = (TextView) convertView.findViewById(R.id.textView4);
TextView tv5 = (TextView) convertView.findViewById(R.id.textView5);
TextView tv6 = (TextView) convertView.findViewById(R.id.textView6);
tv1.setText(list.get(position).getId());
tv2.setText(list.get(position).getTitle());
tv3.setText(list.get(position).getBody());
tv4.setText(list.get(position).getCommentCount());
tv5.setText(list.get(position).getAuthor());
tv6.setText(list.get(position).getAuthoruid2());
return convertView;
}
}
}
==============================utls辅助工具类
package com.example.xmlpull;
import org.apache.http.HttpResponse;
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 android.util.Log;
public class MyHttpUrl {
public static String gethttpurl(String path){
try {
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(path);
HttpResponse exe=client.execute(get);
if(exe.getStatusLine().getStatusCode()==200){
return EntityUtils.toString(exe.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}