访问服务器上的xml文件,通过服务器获取xml文件并解析

本文介绍如何使用Tomcat服务器加载并解析XML文件,通过HTTP请求获取XML数据,并利用自定义适配器将数据展示在Android应用的ListView中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于数据在服务器上

所以使用Tomcat作为服务器

http://localhost:8080能连接上说明启动成功了

把xml文件放在E:\Tomcat9\webapps\ROOT下

xml代码如下

c1cc723069505736249a7e113945f15b.png

由于数据在服务上 所以要开启线程发起网络请求获取数据

private void initData() {

//开启线程发起网络请求

new Thread() {

@Override

public void run() {

super.run();

String path = "http://这里写上你的电脑的ip地址:8080/app.xml";

try {

//要访问的路径

URL url = new URL(path);

//用于发送或接受数据

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

//发送get请求

conn.setRequestMethod("GET");

//请求超时时间

conn.setConnectTimeout(5000);

//服务器返回的状态码

int code = conn.getResponseCode();

//说明请求成功

if (code == 200) {

//获取服务器返回的数据

InputStream in = conn.getInputStream();

//创建一个方法去解析xml文件的逻辑

appsList = ParserXml.parserXml(in);

//把数据展示在Listview上

runOnUiThread(new Runnable() {

@Override

public void run() {

lv_list.setAdapter(new MyAdapter(MainActivity.this));

}

});

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}.start();

}

解析xml文件的逻辑,创建一个集合类,里面放的是Apps的属性

0a526346d8960b913e21a228254bbbc9.png

public class ParserXml {

//用于解析xml数据的方法

public static ListparserXml(InputStream in) {

ListappsList=null;

Apps apps=null;

//获取xml解析器

XmlPullParser Parser = Xml.newPullParser();

try {

//要解析的内容

Parser.setInput(in, "utf-8");

//获取解析的事件类型

int type = Parser.getEventType();

//不等于文档的结尾就不停的向下解析

while (type != XmlPullParser.END_DOCUMENT) {

//判断是开始节点还是结束节点

switch (type) {

//开始节点

case XmlPullParser.START_TAG:

if ("apps".equals(Parser.getName())) {

appsList = new ArrayList<>();

} else if ("app".equals(Parser.getName())) {

apps = new Apps();

} else if ("id".equals(Parser.getName())) {

apps.setId(Parser.nextText());

} else if ("title".equals(Parser.getName())) {

apps.setTitle(Parser.nextText());

} else if ("content".equals(Parser.getName())) {

apps.setContent(Parser.nextText());

} else if ("image".equals(Parser.getName())) {

apps.setImage(Parser.nextText());

} else if ("comment".equals(Parser.getName())) {

apps.setComment(Parser.nextText());

}

break;

//结束节点

case XmlPullParser.END_TAG:

if ("app".equals(Parser.getName())) {

appsList.add(apps);

}

break;

}

type = Parser.next();

}

} catch (XmlPullParserException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return appsList;

}

}

要把数据展示出来就需要一个ListView或RecyclerView

b8c628de3ded131e5781f88069bd9845.png

新建一个类继承 BaseAdapter,这里用到了第三方库smartimageview,用于设置图片的url

public class MyAdapter extends BaseAdapter {

private Context mcontext;

MyAdapter(Context context) {

this.mcontext = context;

}

@Override

public int getCount() {

return appsList.size();

}

@Override

public Object getItem(int position) {

return null;

}

@Override

public long getItemId(int position) {

return 0;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder vh;

if (convertView == null) {

view = View.inflate(mcontext, R.layout.list_apps, null);

vh = new ViewHolder();

vh.iv_icon = view.findViewById(R.id.iv_icon);

vh.tv_title = view.findViewById(R.id.tv_title);

vh.tv_content = view.findViewById(R.id.tv_content);

vh.tv_comment = view.findViewById(R.id.tv_comment);

view.setTag(vh);

} else {

view = convertView;

vh = (ViewHolder) view.getTag();

}

//设置图片

String uriImage = appsList.get(position).getImage();

vh.iv_icon.setImageUrl(uriImage);

//设置标题

vh.tv_title.setText(appsList.get(position).getTitle());

//设置内容

vh.tv_content.setText(appsList.get(position).getContent());

//获取id,根据id去判断访问量的多少

int id = Integer.parseInt(appsList.get(position).getId());

switch (id) {

case 1:

vh.tv_comment.setText(appsList.get(position).getComment());

break;

case 2:

vh.tv_comment.setText(appsList.get(position).getComment());

break;

case 3:

vh.tv_comment.setText(appsList.get(position).getComment());

break;

}

return view;

}

class ViewHolder {

com.loopj.android.image.SmartImageView iv_icon;

TextView tv_title;

TextView tv_content;

TextView tv_comment;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值