源码源码?
有没有源码?
有没有源码?
2015-09-20
Girl类
package comsofteem.xml;
public class Girl {
private String name;
private int age;
private String school;
@Override
public String toString() {
return "Girl [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
XmlThread类
package comsofteem.xml;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Handler;
import android.widget.TextView;
public class XmlThread extends Thread {
private String url;
private TextView textView;
private Handler handler;
public XmlThread(String url, TextView textView, Handler handler) {
this.url = url;
this.textView = textView;
this.handler = handler;
}
@Override
public void run() {
try {
URL httpUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
InputStream is = conn.getInputStream();
//获取XmlPullParserFactory对象
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//获取XmlPullParser对象
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, "utf-8");
//pull解析基于时间驱动
int evenType = parser.getEventType();
final List<Girl> list = new ArrayList<Girl>();
Girl girl = null;
while(evenType!=XmlPullParser.END_DOCUMENT){
String data = parser.getName();
switch (evenType) {
case XmlPullParser.START_TAG:
if("girl".equals(data)){
girl = new Girl();
}
if("name".equals(data)){
girl.setName(parser.nextText());
}
if("age".equals(data)){
girl.setAge(Integer.parseInt(parser.nextText()));
}
if("school".equals(data)){
girl.setSchool(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("girl".equals(data)&&girl!=null){
list.add(girl);
}
break;
}
//取完一个girl对象进入下一个
evenType = parser.next();
}
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
textView.setText(list.toString());
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
XmlActivity类
package comsofteem.xml;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class XmlActivity extends Activity {
private TextView textView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myxml);
textView = (TextView) findViewById(R.id.textView);
String url = "http://172.16.22.54:8080/Second/girls.xml";
new XmlThread(url, textView, handler).start();
}
}
举报