1.首先xUtils-2.4.4.jar包导入libs:
2.实现的.java文件有:
3.代码展示:
3.1、bean.class类
public class Bean {
public String title;
public String body;
public Bean() {
super();
// TODO Auto-generated constructor stub
}
public Bean(String title, String body) {
super();
this.title = title;
this.body = body;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Override
public String toString() {
return "Bean [title=" + title + ", body=" + body + "]";
}
}
3.2 MainActivity.class类
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.widget.TextView;
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;
public class MainActivity extends Activity {
String path = "http://172.17.29.120/localuser/loupengfei/kaoshi/student.xml";
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
initCtrl();
// 获取数据源
getDatas();
}
private void initCtrl() {
tv = (TextView) findViewById(R.id.tv);
}
private void getDatas() {
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
private Bean bean;
// 获取数据失败
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
// 获取数据成功
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String json = arg0.result;
// String 转换成流
InputStream inputStream = new ByteArrayInputStream(json
.getBytes());
// 获得pull解析对象
XmlPullParser newPullParser = Xml.newPullParser();
try {
// 设置输入流
newPullParser.setInput(new InputStreamReader(inputStream));
// 得到标签的类型
int eventType = newPullParser.getEventType();
ArrayList<Bean> list = new ArrayList<Bean>();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
System.out.println("开始文件标签");
break;
case XmlPullParser.START_TAG:
System.out.println("开始标签");
String tagName = newPullParser.getName();
if (tagName.equals("student")) {
bean = new Bean();
} else if (tagName.equals("name")) {
String title = newPullParser.nextText();
bean.title = title;
System.out.println("address"
+ "--------------------" + title);
} else if (tagName.equals("address")) {
String body = newPullParser.nextText();
bean.body = body;
System.out
.println("body"
+ "____________________________"
+ body);
}
break;
case XmlPullParser.END_TAG:
System.out.println("开始文件标签");
tagName = newPullParser.getName();
if (tagName.equals("student")) {
list.add(bean);
}
break;
default:
break;
} // 获得下一个标签类型勿忘
eventType = newPullParser.next();
}
String result = "";
for (Bean b : list) {
result += b.toString();
}
tv.setText(result);
System.out.println(list.toString());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
还有 注意一点别忘了加网络权限
<uses-permission android:name="android.permission.INTERNET" />