这个实体类
package com.nico.entity;
public class UserInfo {
public String name;
public String id;
public String gender;
}
解析相关类
package com.nico.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.util.Xml;
import com.nico.entity.UserInfo;
public class ParserXml {
private UserInfo user;
private ArrayList<UserInfo> list;
InputStream is = null;
String url;
public ParserXml() {
// this.url = url;
// act = actt;
}
public ArrayList<UserInfo> getInfo(InputStream is) {
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(is, "utf-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<UserInfo>();
break;
case XmlPullParser.START_TAG:
if ("userinfo".equals(parser.getName())) {
user = new UserInfo();
user.id =parser.getAttributeValue(null,"id");
// user.id = parser.getAttributeValue(0);
}
if ("username".equals(parser.getName())) {
user.name = parser.nextText();
System.out.println(user.name);
}
if ("usergender".equals(parser.getName())) {
user.gender = parser.nextText();
}
break;
case XmlPullParser.END_TAG:
if ("userinfo".equals(parser.getName())) {
System.out.println("user == null>>>>>"+(user==null));
list.add(user);
}
break;
}
event = parser.next();
}
} catch (Exception ex) {
}
return list;
}
}
MainActivity
package com.nico;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.nico.entity.UserInfo;
import com.nico.util.ParserXml;
public class Test extends Activity {
public ArrayList<UserInfo> list;
private LayoutInflater inflater;
private Adapter adapter ;
private Handler mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
// ParserXml parser = new ParserXml(
// "http://10.0.0.130:8080/info.xml");
try {
ParserXml parser = new ParserXml();
URL u = new URL("http://10.0.0.130:8080/info.xml");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
InputStream is = conn.getInputStream();
list = parser.getInfo(is);
} catch (Exception e) {
e.printStackTrace();
}
adapter = new Adapter();
listview.setAdapter(adapter);
}
}
};
private ListView listview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater = getLayoutInflater();
listview = (ListView) findViewById(R.id.list);
// Adapter adapter = new Adapter();
// mhandler.post(new Runnable() {
//
// @Override
// public void run() {
// ParserXml parser = new ParserXml(
// "http://10.0.0.130:8080/info.xml");
// list = parser.getInfo();
// }
// });
mhandler.sendEmptyMessage(1);
}
public class Adapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View v = inflater.inflate(R.layout.item, null);
TextView id = (TextView) v.findViewById(R.id.id);
TextView gender = (TextView) v.findViewById(R.id.gender);
TextView name = (TextView) v.findViewById(R.id.name);
// System.out.println(list.get(arg0).id + "" + " "
// + list.get(arg0).gender + "" + " " + list.get(arg0).name);
id.setText(list.get(arg0).id + "");
gender.setText(list.get(arg0).gender + "");
name.setText(list.get(arg0).name);
return v;
}
}
}
Item xml<都是最简单的了>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="60dip"
android:layout_height="wrap_content"
android:id="@+id/id"/>
<TextView android:layout_width="130dip"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView android:layout_width="130dip"
android:layout_height="wrap_content"
android:id="@+id/gender"/>
</LinearLayout>
主Main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="60dip" android:layout_height="wrap_content"
android:text="id" />
<TextView android:layout_width="130dip"
android:layout_height="wrap_content" android:text="name" />
<TextView android:layout_width="130dip"
android:layout_height="wrap_content" android:text="gender" />
</LinearLayout>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/list" />
</LinearLayout>
都是最简单的了,用来解析xml信息就不贴了。
大概意思就是联网解析一个xml文件,并在view中展示。
没有用什么图片信息的。