由于数据在服务器上
所以使用Tomcat作为服务器
http://localhost:8080能连接上说明启动成功了
把xml文件放在E:\Tomcat9\webapps\ROOT下
xml代码如下
由于数据在服务上 所以要开启线程发起网络请求获取数据
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的属性
public class ParserXml {
//用于解析xml数据的方法
public static List<Apps> parserXml(InputStream in) {
List<Apps> appsList=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
新建一个类继承 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;
}
}