Android解析XML(PULL)展示到ListView
程序员文章站
2023-12-17 20:18:22
android解析xml展示到listview运行后的效果图如下:
服务端的请求页面
<%@ page language="java" cont...
android解析xml展示到listview运行后的效果图如下:
服务端的请求页面
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <a href="studentactiongetxml.action" rel="external nofollow" >获取xml数据</a><br/> </body> </html>
服务端返回结果的页面
<?xml version="1.0" encoding="utf-8" ?> <%@ page language="java" contenttype="text/xml; charset=utf-8" pageencoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <students> <c:foreach items="${students}" var="s"> <student name="${s.name}"> <sex>${s.sex}</sex> </student> </c:foreach> </students>
服务端的java代码
package com.zking.action; import java.util.arraylist; import java.util.list; import org.apache.struts2.servletactioncontext; import com.opensymphony.xwork2.actionsupport; import com.zking.entity.student; public class studentaction extends actionsupport{ public string getxml() throws exception { //查询数据库,获取数据 list<student> students=new arraylist<>(); for (int i = 1; i <=20; i++) { student student=new student("小霜"+i, "女"); students.add(student); } //将对象集合保存到请求域中 servletactioncontext.getrequest().setattribute("students", students); return "dataresult"; } }
服务端的配置文件(struts.xml)
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.3//en" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="mypackage" extends="struts-default"> <action name="studentaction*" class="com.zking.action.studentaction" method="{1}"> <result name="dataresult">/dataresult.jsp</result> </action> </package> </struts>
服务端的运行结果
android (布局文件 activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.g150825_android29_parsexml.mainactivity"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取xml" android:onclick="getxml" /> <listview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_main_list" ></listview> </linearlayout>
android(java代码 mainactivity)
package com.example.g150825_android29_parsexml; import android.app.progressdialog; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.util.xml; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; import org.xmlpull.v1.xmlpullparser; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { list<student> studentlist=new arraylist<>(); private listview lv_main_list; private myadater myadater; private progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //实例化进度条对话框 progressdialog = new progressdialog(this); progressdialog.setmessage("正在拼命加载中....."); lv_main_list = (listview) findviewbyid(r.id.lv_main_list); //实例化适配器 //设置适配器 myadater = new myadater(); lv_main_list.setadapter(myadater); } class myadater extends baseadapter{ @override public int getcount() { return studentlist.size(); } @override public object getitem(int i) { return studentlist.get(i); } @override public long getitemid(int i) { return i; } @override public view getview(int i, view view, viewgroup viewgroup) { linearlayout linearlayout=new linearlayout(mainactivity.this); linearlayout.setorientation(linearlayout.horizontal); textview textviewname=new textview(mainactivity.this); textviewname.settext(studentlist.get(i).getname()); textview textviewsex=new textview(mainactivity.this); textviewsex.settext(studentlist.get(i).getsex()); linearlayout.addview(textviewname); linearlayout.addview(textviewsex); return linearlayout; } } public void getxml(view view){ new mytask().execute(); } class mytask extends asynctask{ private student student; @override protected void onpreexecute() { super.onpreexecute(); progressdialog.show(); } @override protected object doinbackground(object[] objects) { //01.确定网络数据 string path="http://192.168.43.152:8080/g150825_s2sh/studentactiongetxml.action"; try { //02.实例化url url url=new url(path); //03.获取连接对象 httpurlconnection httpurlconnection= (httpurlconnection) url.openconnection(); //04.设置请求方式 httpurlconnection.setrequestmethod("get"); //05.设置请求连接超时的时间(优化) httpurlconnection.setconnecttimeout(5000); //06.获取响应码(结果码) int code=httpurlconnection.getresponsecode(); if (code==200){ //07.获取服务器返回过来的数据 inputstream is=httpurlconnection.getinputstream(); //测试(打印) //缓冲字符流 // bufferedreader br=new bufferedreader(new inputstreamreader(is)); // string str=null; // while ((str=br.readline())!=null){ // log.i("test",str); // } //解析xml(pull) xmlpullparser xmlpullparser=xml.newpullparser(); xmlpullparser.setinput(is,"utf-8"); int type=xmlpullparser.geteventtype(); while (type!=xmlpullparser.end_document){ switch (type){ case xmlpullparser.start_tag: //获取开始标签 string starttagname=xmlpullparser.getname(); if ("student".equals(starttagname)){ student = new student(); //获取name属性值 string name=xmlpullparser.getattributevalue(0); student.setname(name); }else if("sex".equals(starttagname)){ //获取sex的文本值 string sex=xmlpullparser.nexttext(); student.setsex(sex); } break; case xmlpullparser.end_tag: //获取到结束标签的名字 string endtagname=xmlpullparser.getname(); if("student".equals(endtagname)){ studentlist.add(student); } break; } type=xmlpullparser.next(); } } } catch (exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(object o) { super.onpostexecute(o); //通知适配器发生改变 myadater.notifydatasetchanged(); progressdialog.cancel(); } } }
android (实体类 student)
package com.example.g150825_android29_parsexml; public class student { private string name; private string sex; public student() { super(); // todo auto-generated constructor stub } public student(string name, string sex) { super(); this.name = name; this.sex = sex; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } }
在清单文件中添加权限即可(androidmanifest.xml)
<!--添加网络权限--> <uses-permission android:name="android.permission.internet"></uses-permission>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读