《Android网络应用程序开发.docx》由会员分享,可在线阅读,更多相关《Android网络应用程序开发.docx(36页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Android网络应用程序开发实验指导书软件学院软件工程系包哈Overridepublic void characters(char ch, int start, int length)throws SAXException if (person != null) String valueString = new String(ch5 start, length);if (NAMESTRING.equals(tempString) /如果当前解析到的节点是name就要将name中的文本节点 元素的值得到person.setName(valueString); else if (AGESTRIN
2、G.equals(tempString) person.setAge(new lnteger(valueString).intValue();)/* sax读取到元素节点的时候用到这个方法;* /Overridepublic void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException /先判断读到的元素是否是personif (PERSONSTRING.equals(localName)/如果读到的是person这个元素 就要保存起来,保存在我们创
3、建 的那个person的类中 所以莪们要new 一个person类person = new Person();/ attributes 是属性。person.setld(new lnteger(attributes.getValue(IDSTRING);)tempString = localName;* 这个方法是每次遇到结束的标签都会执行的并不是只遇到最后的结尾才调 用* 读取完毕遇到person,的结尾 就将封装好的一个personbean保存到list中 并且清空person对象* /Overridepublic void endElement(String uri, String lo
4、calName, String name)throws SAXException if(PERSONSTRING.equals(localName)&person!=null)persons.add(person);person = null;tempString = null;SaxReadxmlTest.javapackage com.leequer.readxml;/* 测试类* /import java.io.Inputstream;import java.utiLlterator;import java.util.List;import com.leequer.service.Sax
5、Readxml;import com.leequer.service.demo.Person;import android.test.AndroidTestCase;import android.util.Log;public class SaxReadxmlTest extends AndroidTestCase private String PERSONSTRING = HObjectPersonH;public void testReadXml() throws Exception类装载器Inputstreaminputstream=SaxReadxmlTest.class.getCla
6、ssLoader().getResourceAsStream(nNewFile.xmlList personsList = SaxReadxmLreadXml(inputStream);for (Iterator iterator = personsList.iterator。; iterator.hasNext。;) Person person = (Person) iterator.next();Log.i(PERSONSTRING5 person.toString();实验三DOM解析xml【实验类型】验证性【实验要求】必做【实验目的】1 . 了解D0M的运行原理。2 .掌握D0M解析x
7、ml的步骤。【实验内容】本实验和实验四PULL解析xml共享同一个xml文件,故放在一个工程中。此两次 实验采用调试的方法检验结果,解析信息查看Logcat。工程的结构如图:Project Explorer EE二:三 ReadXmlByDomAndPull0 srccom.android.helenj ReadXmlByDaom.java田 com.android.helen.beansj M S N Co nta ct.j avaj MSNGroup.java田 com.android.helen.servicej M S N Co nta ctCo m pa rato r.j ava,J
8、j MSNContactDom.java ,Jj MSNContactXmlPull.java田 com.android.helen.test由 MSNDomXmlTest.java|J| MSNPullXmlTest.javaX msncontacts.xml当 gen Generated Java Files4 Android 2.1三 JRE System Library jdkl,6,0_26loii dom4j-1.6.jar勇 assets心bin0 lib方res AndroidManifest.xml曾 project.propertiesBl readme.txt【参考程序】
9、 msncontacts. xmll aaFri3con impresacon email2bbbbFri4con bb impresacon bb email21 aaFri7con impresacon email22bbbbFri5con bb impresacon bb emai 1 24ccbbFri6con bb impresacon bb emailMSNContact.javapackage com.android.helen.beans;public class MSNContact private String groupID;private String contactI
10、D;private String contactDisplayName;private String contactFriendlyName;private String contactstatus;private String contactlmpresa;private String contactEmail;public String getGIDString() (return ngrouplDn;)public String getCIDString()(return HcontactlDn;)public String getCDNameString()(return Hconta
11、ctDisplayNameM;)public String getCFNameString()(return HcontactFriendlyNameM;public String getCStatusString()(return contactstatus”;)public String getClmpresaString()(return McontactlmpresaH;)public String getCEmailString()(return “contact Em ail”;)public String getGrouplD() return groupID;)public v
12、oid setGrouplD(String groupID) this.groupID = groupID;public String getContactlD() return contactID;)public void setContactlD(String contact ID) this.contactID = contactID;)public String getContactDisplayName() return contactDisplayName;)public void setContactDisplayName(String contactDisplayName) t
13、his.contactDisplayName = contactDisplayName;)public String getContactFriendlyName() return contactFriendlyName;)public void setContactFriendlyName(String contactFriendlyName) this.contactFriendlyName = contactFriendlyName;)public String getContactStatus() return contactstatus;)public void setContact
14、Status(String contactstatus) this.contactstatus = contactstatus;)public String getContactlmpresa() return contactlmpresa;)public void setContactlmpresa(String contactlmpresa) this.contactlmpresa = contactlmpresa;)public String getContactEmail() return contactEmail;)public void setContactEmail(String
15、 contactEmail) this.contactEmail = contactEmail;)public String toString()(return contact: groupID = + groupID+ ”,conlD = + contact ID+ ”,conDisplayName = + contactDisplayName+ , conFriendlyName = + contactFriendlyName+ ”,conStatus = + contactstatus+ ”,conlmpresa = + contactlmpresa+ ”,conEmail = + co
16、ntactEmail;)MSNGroup.javapackage com.android.helen.beans;public class MSNGroup private String groupID;private String groupName;private String groupConCount;public String getGIDString()(return ngrouplDH;)public String getGNameString()(return HgroupName;)public String getGConCountString()(return ngrou
17、pConCountn;)public String getGrouplD() return groupID;)public void setGrouplD(String groupID) this.groupID = groupID;)public String getGroupName() return groupName;)public void setGroupName(String groupName) this.groupName = groupName;)public String getGroupConCount() return groupConCount;)public vo
18、id setGroupConCount(String groupConCount) this.groupConCount = groupConCount;public String toString()(return group: id = + groupID+ ”,name = + groupName+ , conCount = + groupConCount;)MSN ContactCom parato rjavapackage com.android.helen.service;import java.utiLComparator;import com.android.helen.bea
19、ns.MSNContact;public class MSNContactComparator implements Comparator public int compare(MSNContact o1, MSNContact o2) /TODO Auto-generated method stubMSNContact coni = (MSNContact)ol;MSNContact con2 = (MSNContact)o2;/online 1, others more than 1if(!con1 .getContactStatus().equals(con2.getContactSta
20、tus()returncon1.getContactStatus().compareTo(con2.getContactStatus();else /if coni and con2 are in the same status, then sort by name returncon1.getContactFriendlyName().compareTo(con2.getContactFriendlyName();)MSNContactDom.javapackage com.android.helen.service;import java.io.File;import java.io.Fi
21、leWriter;import java.io.Inputstream;import java.utiLArrayList;import java.utiLCollections;import java.utiLComparator;import java.utiLHashSet;import java.util.lterator;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.dom4j.Document;import org.dom4j.D
22、ocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import com.android.helen.beans.MSNContact;import com.android.helen.beans.MSNGroup;import android.util.Log;public class MSNContactDom public ArrayList gpList = new Arr
23、ayList();public ArrayList conList = new ArrayList();private String filename =public MSNContactDom()(filename = msnxmr;/1Z4 Jpublic void initGpAndConList()try (File file = new File(filename);SAXReader rd = new SAXReader();Document doc = rd.read(file);Element root = doc.getRootElement();/group element
24、Element elGp;for(lterator i = root.elementlterator(ngroupH);i.hasNext();)/get elementelGp = (Element)i.next();/init one msngroup, and set attributesMSNGroup mg = new MSNGroup();mg.setGrouplD(elGp.attribute Value(mg.getGIDString();mg.setGroupName(elGp.attribute Value(mg.getGNameString();mg.setGroupCo
25、nCount(elGp.attribute Value(mg.getGConCountString();gpList.add(mg);/get contactsElement elCon;for(lterator j = elGp.elementlterator(contactH);j.hasNext();) elCon = (Element)j.next();/init one contact, and set attributes MSNContact me = new MSNContact(); mc.setGrouplD(mg.getGrouplD();mc.setContactlD(
26、elCon.elementText(mc.getCIDString();mc.setContactDisplayName(elCon.elementText(mc.getCDNameString();mc.setContactFriendlyName(elCon.elementText(mc.getCFNameString();mc.setContactStatus(elCon.elementText(mc.getCStatusString();mc.setContactlmpresa(elCon.elementText(mc.getClmpresaString();mc.setContact
27、Email(elCon.elementText(mc.getCEmailString();conList.add(mc);) catch (DocumentException e) / TODO Auto-generated catch blocke.printStackTrace();public boolean hasDom()(File f = new File(filename);if(f.exists()return true;elsereturn false;)public ArrayList getGroupCon(String groupID)(ArrayList groupC
28、on = new ArrayList();for(lterator i = conList.iterator(); i.hasNext();)MSNContact con = (MSNContact)i.next();实验内容实验一android网络基础【实验类型】验证性【实验要求】必做【实验目的】1. 了解Tomcat、j2ee的安装、配置、使用。2,熟悉jsp基本指令。3.熟悉jsp常用内置对象。【实验内容】一、1、下载一个带j2ee插件的eclipse,下载方法百度一下,你就知道!安装好后, 安装最新的ADT插件,参照Android基础指导书。2、下载一个tomcat,从apache的官
29、方网站上下载一个,下最新版本7. x版本,地址 http:/tomcat, apache, org/,下载windows版的,下载后exe文件一溜安装,安装内容部 分的选项全选,其他全部默认配置,中间有个admin密码的地方,输入你自己设定的密码即 可。安装后启动,如果启动不了,请检查安装的jdk版本,也换成最新的重新安装。安装完 了后,服务启动tomcat,浏览器输入,http:/localhost:8080/, ok,页面出来正常表示。3,下载tomcat对eclipse的插件。下载土也土止:http:/www. eclipsetotale. com/tomcatPlugin. html
30、这个页面还有,安装说明的,英语好的可以自己看看。下载的时候针对你安装的eclipse 的版本下载,下载后解压,解压后的文件放到eclipse的plugins目录中,如果eclipse 是3. 7版本的放到dropins文件夹。配置完毕,应用按钮按下,关闭,然后先从window的服务中停止tomcat的服 务,回到eclipse的页面,点那三个图标,ok 了吧,下面的console的窗口有反映 了吧,然后打开浏览器,http:localhost:8080/ok 了, tomcat 启动了if(con.getGrouplD().equals(grouplD)groupCon.add(con);)r
31、eturn groupCon;)public ArrayList sortGroupCon(String groupID)(A r ray Li st groupCon = new Array List();groupCon = getGroupCon(grouplD);/sort group contact by status,/if in the same status, then sort by friNameComparator comp = new MSNContactComparator();Collections.sort(groupCon,comp);/f o r (I te
32、rato r j = groupCon.iterator(); j.hasNext();)/MSNContact con2 = (MSNContact)j.next();/Log.e(nmsnxmr,con2.toString();/return groupCon;)public boolean createDom() throws Exception(/create docDocument doc = DocumentHelper.createDocument();/create rootElement root = doc.addElement(Hgroupsn);/ StringstrM
33、c=newStringmc.getCIDString(),mc.getCDNameString()3/mc.getCFNameString(),mc.getCStatusString(),/mc.getClmpresaString(),mc.getCEmailString();/create elements according gpListfor(int i = 0; i gpList.size(); i+)(/create group and set attributesMSNGroup mg = (MSNGroup)gpList.get(i);Element elGroup = root
34、.addElement(HgroupH);elGroup.addAttribute(mg.getGIDString()J mg.getGrouplD();elGroup.addAttribute(mg.getGNameString(), mg.getGroupName();elGroup.addAttribute(mg.getGConCountString(), mg.getGNameString();/get contact list by groupIDA r ray Li st cons = getGroupCon(mg.getGrouplD(); /create contactElem
35、ent elCon = elGroup.addElement(contactH);/set contact attributesfor(int j = 0; j cons.size(); j+)(MSN Contact me = new MSNContact();me = cons.get(i);elCon.addElement(mc.getCIDString(), mc.getContactlD();elCon.addElement(mc.getCDNameString(), mc.getContactDisplay Name();elCon.addElement(mc.getCFNameS
36、tring()3 mc.getCFNameStringO);elCon.addElement(mc.getCStatusString(), mc.getContactStatus(); elCon.addElement(mc.getClmpresaString(),mc.getContactlmpresaO);elCon.addElement(mc.getCEmailString(), mc.getContactEmail();try XMLWriter wr = new XMLWriter(new FileWriter(newFile(filename);wr.write(doc);wr.c
37、lose();return true; catch (Exception e) / TODO Auto-generated catch block e.printStackTrace();Loge(msnxmJcreate dom ex = + e);return false;public void updateOneContactFromList(MSNContact me) (for(lterator i = conList.iterator();i.hasNext();)(MSNContact mmc = (MSNContact)i.next();if(mmc.getCIDString(
38、).equals(mc.getCIDString()mmc.setContactFriendlyName(mc.getContactFriendlyName();mmc.setContactlmpresa(mc.getContactlmpresa();mmc.setContactStatus(mc.getContactStatus();public boolean writeDomBeforeLeave() (boolean isSucc = true;/delete old domFile file = new File(filename);file.delete();/create new
39、 domreturn isSucc;public ArrayList getGpList() return gpList;)public void setGpList(ArrayList gpList) this.gpList = gpList;)public ArrayList getConList() return conList;public void setConList(ArrayList conList) this.conList = conList;)MSNDomXmlTest.javapackage com.android.helen.test;import java.utiL
40、ArrayList;import java.util.lterator;import android.test.AndroidTestCase;import android.util.Log;import com.android.helen.beans.MSNContact;import com.android.helen.beans.MSNGroup;import com.android.helen.service.MSNContactDom;public class MSNDomXmlTest extends AndroidTestCase private String MSNXML =
41、msnxml”;public void testReadXml() throws Exception 类装载器MSNContactDom med = new MSNContactDom();boolean createDom = true;if(mcd.hasDom() (Log.d(Hmsnxmr,HisExistsH);createDom = mcd.createDom();if(createDom)(mcd.initGpAndConList();ArrayList mgList = mcd.gpList;A r ray Li st conList = new Array List();for (lterator iterator = mgList.iterator(); iterator.hasNext();) (MSNGroup mg = (MSNGroup) iterator.next();Log.i(MSNXML, mg.toString();conList = mcd.sortGroupCon(mg.getGrouplD();for (Iterator i = conList.iterator(); i.hasNext();) MSNContact con = (MSNContact) i.next();Lo
限制150内