JavaSE|Android(网络:获取XML格式数据)
【JavaSE|Android(网络:获取XML格式数据)】public class News {
private Integer id;
private String title;
private Integer timelength;
...
}
ListView listView = (ListView) this.findViewById(R.id.listView);
try {
List
List data = https://www.it610.com/article/new ArrayList();
for(News news : videos){
HashMap item = new HashMap();
item.put("id", news.getId());
item.put("title", news.getTitle());
item.put("timelength", getResources().getString(R.string.timelength)
+ news.getTimelength()+ getResources().getString(R.string.min));
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"title", "timelength"}, new int[]{R.id.title, R.id.timelength});
listView.setAdapter(adapter);
public static List
String path = "http://192.168.0.168:8080/web/ListServlet";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream inStream = conn.getInputStream();
return parseXML(inStream);
}
return null;
}
/**
* 解析服务器返回的xml数据
* @param inStream
* @return
*/
private static List
List
News news = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inStream, "UTF-8");
int event = parser.getEventType();
while( event != XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
if("news".equals(parser.getName())){
int id = new Integer(parser.getAttributeValue(0));
news = new News();
news.setId(id);
}else if("title".equals(parser.getName())){
news.setTitle(parser.nextText());
}else if("timelength".equals(parser.getName())){
news.setTimelength(new Integer(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if("news".equals(parser.getName())){
newses.add(news);
news = null;
}
break;
}
event = parser.next();
}
return newses;
}
服务器端
Servlet:
List
request.setAttribute("videos", videos);
request.getRequestDispatcher("/WEB-INF/page/videonews.jsp").forward(request, response);
public List
List
newes.add(new News(90, "喜羊羊与灰太狼全集", 78));
newes.add(new News(10, "实拍舰载直升东海救援演习", 28));
newes.add(new News(56, "喀麦隆VS荷兰", 70));
return newes;
}
JSP:
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- android第三方框架(五)ButterKnife
- 猎杀IP
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- 自媒体形势分析
- 数学大作战
- 2018.03.18
- Android事件传递源码分析