HttpClient、XmlPullParser、ListView与BaseAdapter的综合用法
知识点总结
1、通过android API内置的类HttpClient从后台取出数据,HttpClient是一个接口,所以用他的子类来实例化DefaultHttpClient,具体的代码如下:
private List<News> getNewsFromInternet() {
HttpClient client = null;
try {
// 定义一个客户端
client = new DefaultHttpClient();
// 定义get方法
HttpGet get = new HttpGet("http://10.0.2.2:8080/NetEaseServer/new.xml");
// 执行请求
HttpResponse response = client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200) {
InputStream is = response.getEntity().getContent();
List<News> newInfoList = getNewListFromInputStream(is);
return newInfoList;
} else {
Log.i(TAG, "访问失败: " + statusCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(client != null) {
client.getConnectionManager().shutdown(); // 关闭和释放资源
}
}
return null;
}
3、XmlPullParser解析从后来发送过来的XML文件
private List<News> getNewListFromInputStream(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser(); // 创建一个pull解析器
parser.setInput(is, "utf-8"); // 指定解析流, 和编码
int eventType = parser.getEventType();
List<News> newInfoList = null;
News newInfo = null;
while(eventType != XmlPullParser.END_DOCUMENT) { // 如果没有到结尾处, 继续循环
String tagName = parser.getName(); // 节点名称
switch (eventType) {
case XmlPullParser.START_TAG: // <news>
if("news".equals(tagName)) {
newInfoList = new ArrayList<News>();
} else if("new".equals(tagName)) {
newInfo = new News();
} else if("title".equals(tagName)) {
newInfo.setTitle(parser.nextText());
} else if("detail".equals(tagName)) {
newInfo.setDetail(parser.nextText());
} else if("comment".equals(tagName)) {
newInfo.setComment(parser.nextText());
} else if("image".equals(tagName)) {
newInfo.setBitmap(parser.nextText());
}
break;
case XmlPullParser.END_TAG: // </news>
if("new".equals(tagName)) {
newInfoList.add(newInfo);
}
break;
default:
break;
}
eventType = parser.next(); // 取下一个事件类型
}
return newInfoList;
}
3、绑定到ListView这个控件需要设置setAdapter的属性,属先要定义一个类来实现BaseAdapter,主要来实现BaseAdapter的二个方法,具体代码如下:
class Myadapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return listnewsinfo.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) {
View view = null;
if(convertView == null) {
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.activity_item, null);
} else {
view = convertView;
}
// 重新赋值, 不会产生缓存对象中原有数据保留的现象
SmartImageView sivIcon = (SmartImageView) view.findViewById(R.id.iv_itemimg_icon);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_item_tvtitle);
TextView tvDetail = (TextView) view.findViewById(R.id.tv_item_discuss);
TextView tvComment = (TextView) view.findViewById(R.id.tv_item_content);
News newInfo = listnewsinfo.get(position);
sivIcon.setImageUrl(newInfo.getBitmap()); // 设置图片
tvTitle.setText(newInfo.getTitle());
tvDetail.setText(newInfo.getDetail());
tvComment.setText(newInfo.getComment() + "跟帖");
return view;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。