Junit Test 就是单元测试,要自己编写代码来检测代码的正确性
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <span style="font-size:18px;background-color: rgb(255, 102, 102);"><uses-library android:name="android.test.runner"/></span> <activity android:name="com.example.helllojunit.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <span style="color:#ff0000;"> <span style="font-size:18px;"><instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.helllojunit" android:label="hello test" /></span></span>如上2点要注意的 见红色标注
然后新建一个类继承AndroidTestCase
public class PersonService { public void sace(){ String string = "2014"; Integer integer = new Integer(2014); } public void sace2(){ String string = "2014"; Integer integer = new Integer(2014); } }
<span style="font-size:14px;">public class PersonServiceTest extends AndroidTestCase{ private static final String TAG = "PersonServiceTest"; public void testSave() throws Throwable{ PersonService personService = new PersonService(); personService.sace(); } public void testwanwan() throws Throwable{ PersonService personService = new PersonService(); personService.sace2(); }</span>
<span style="font-size:14px;"> </span>
<span style="font-size:14px;">里面的方法要以test开头,public修饰 还要抛出异常</span>
<span style="font-size:14px;"><img src="http://img.blog.csdn.net/20141209155934273?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> </span>然后 右击函数,点击run as ----> Android Junit 然后运行结果就是:
如果报错,会跟踪到对应的报错信息。这里就不累赘,可以自行测试
XML解析主要有3个方法: SAX , DOM , Pull
Android内置推荐使用pull 的方法, DOM解析时要全部加载多内存,会耗较多内存, Android设备不推荐使用:
现在使用SAX 跟Pull分别解析下面的2个XML
<?xml version="1.0" encoding="utf-8"?> <persons> <person id="20"> <name>hewence</name> <age>24</age> </person> <person id="21"> <name>zhangzhenlai</name> <age>25</age> </person> </persons>
Pull方法:
<pre name="code" class="java">private static final String TAG_PERSON = "person"; private static final String TAG_NAME = "name"; private static final String TAG_AGE = "age"; private static final String ATTR_ID = "id"; private static final String TAG = null; private static Person person; public static ArrayList<Person> getPerson(InputStream is){ Log.i(TAG , "getPerson = is" + is); if (null == is){ return null; } ArrayList<Person> persons = null ; XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(is, "utf-8"); int event = parser.getEventType(); while(event != XmlPullParser.END_DOCUMENT){ switch (event) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: String tagName = parser.getName(); if (TAG_PERSON.equals(tagName)){ person = new Person(); person.setId(Integer.parseInt(parser.getAttributeValue(0))); }else if (TAG_NAME.equals(tagName)){ person.setName(parser.nextText()); }else if (TAG_AGE.equals(tagName)){ person.setAge(Integer.parseInt(parser.nextText())); } break; case XmlPullParser.END_TAG: tagName = parser.getName(); if (TAG_PERSON.equals(tagName)){ persons.add(person); } break; default: break; } event = parser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return persons; } private static final String TAG_APP = "app"; private static final String TAG_APPINFO = "appinfo"; private static final String TAG_DOWN_URL = "Down_URL"; private static final String TAG_VERSION = "version"; private static final String ATTR_VER = "ver"; // private static final String ATTR_ID = "id"; private static final String ATTR_APP_NAME = "APP_NAME"; private static final String ATTR_APP_VERSION = "APP_VERSION"; private static final String ATTR_ICON_LOCAL = "icon_local"; private static final String ATTR_PACK_NAME = "packname"; private static final String ATTR_APP_URL = "app_url"; public static List<Item> getItems(InputStream is){ if (null == is){ return null; } List<Item> items = null ; Item item = null; XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(is, "utf-8"); int event = parser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_DOCUMENT: items = new ArrayList<Item>(); break; case XmlPullParser.START_TAG: String tagName = parser.getName(); if (TAG_APP.equals(tagName)){ item = new Item(); }else if (TAG_APPINFO.equals(tagName)){ int length = parser.getAttributeCount(); for (int i = 0 ; i < length ; i++){ String attrName = parser.getAttributeName(i); if (ATTR_ID.equals(attrName)){ item.setId(Integer.parseInt(parser.getAttributeValue(i))); }else if (ATTR_APP_NAME.equals(attrName)){ item.setTitle(parser.getAttributeValue(i)); }else if (ATTR_APP_VERSION.equals(attrName)){ item.setVersion(parser.getAttributeValue(i)); }else if (ATTR_ICON_LOCAL.equals(attrName)){ item.setIcon_loacl(Boolean.parseBoolean(parser.getAttributeValue(i))); }else if (ATTR_PACK_NAME.equals(attrName)){ item.setPackageName(parser.getAttributeValue(i)); } } }else if (TAG_DOWN_URL.equals(tagName)){ int length = parser.getAttributeCount(); for (int i = 0 ; i < length ; i++){ String attrName = parser.getAttributeName(i); if (ATTR_APP_URL.equals(attrName)){ item.setAppUrl(parser.getAttributeValue(i)); } } }else if (TAG_VERSION.equals(tagName)){ int length = parser.getAttributeCount(); for (int i = 0 ; i < length ; i++){ String attrName = parser.getAttributeName(i); if (ATTR_VER.equals(attrName)){ Log.i(TAG , " Version = " + parser.getAttributeValue(i)); } } } break; case XmlPullParser.END_TAG: tagName = parser.getName(); if (TAG_APP.equals(tagName)){ items.add(item); } break; default: break; } event = parser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return items; }
public class SaxForParse extends DefaultHandler{ private static final String TAG = "SaxForParse"; private static final String TAG_PERSON = "person"; private static final String TAG_NAME = "name"; private static final String TAG_AGE = "age"; private static final String ATTR_ID = "id"; private List<Person> persons ; private Person person ; private String perTag = ""; @Override public void startDocument() throws SAXException { Log.i(TAG , "startDocument == "); persons = new ArrayList<Person>(); } public List<Person> getPersons() { return persons; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { Log.i(TAG , "startElement == " + "uri = " + uri + "localName = " + localName + " qName = " + qName ); if (TAG_PERSON.equals(localName)){ person = new Person(); int length = attributes.getLength(); for(int i = 0 ; i < length ; i++){ Log.i(TAG , "startElement == attributes name = " + attributes.getLocalName(i) + "values = " + attributes.getValue(i)); if (ATTR_ID.equalsIgnoreCase(attributes.getLocalName(i))){ person.setId(Integer.parseInt(attributes.getValue(i))); } } } perTag = localName; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { Log.i(TAG , "endElement == " + "uri = " + uri + "localName = " + localName + " qName = " + qName ); if (TAG_PERSON.equals(localName)){ persons.add(person); } perTag = null; } @Override public void characters(char[] ch, int start, int length) throws SAXException { String data = new String(ch , start , length).trim(); if (TAG_NAME.equals(perTag)){ person.setName(data); }else if (TAG_AGE.equals(perTag)){ person.setAge(Integer.parseInt(data)); } } @Override public void endDocument() throws SAXException { Log.i(TAG , "endDocument == "); } }
public class SaxForParse2 extends DefaultHandler{ private static final String TAG = "SaxForParse"; private static final String TAG_APP = "app"; private static final String TAG_APPINFO = "appinfo"; private static final String TAG_DOWN_URL = "Down_URL"; private static final String TAG_VERSION = "version"; private static final String ATTR_VER = "ver"; private static final String ATTR_ID = "id"; private static final String ATTR_APP_NAME = "APP_NAME"; private static final String ATTR_APP_VERSION = "APP_VERSION"; private static final String ATTR_ICON_LOCAL = "icon_local"; private static final String ATTR_PACK_NAME = "packname"; private static final String ATTR_APP_URL = "app_url"; private List<Item> items ; private Item item ; // 当前item public List<Item> getItems() { return items; } @Override public void startDocument() throws SAXException { Log.i(TAG, "startDocument"); items = new ArrayList<Item>(); } @Override public void endDocument() throws SAXException { Log.i(TAG, "endDocument"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (TAG_APP.equals(localName)){ item = new Item(); }else if (TAG_APPINFO.equals(localName)){ int length = attributes.getLength(); for (int i = 0 ; i < length ; i++){ String attrName = attributes.getLocalName(i); if (ATTR_ID.equals(attrName)){ item.setId(Integer.parseInt(attributes.getValue(i))); }else if (ATTR_APP_NAME.equals(attrName)){ item.setTitle(attributes.getValue(i)); }else if (ATTR_APP_VERSION.equals(attrName)){ item.setVersion(attributes.getValue(i)); }else if (ATTR_ICON_LOCAL.equals(attrName)){ item.setIcon_loacl(Boolean.parseBoolean(attributes.getValue(i))); }else if (ATTR_PACK_NAME.equals(attrName)){ item.setPackageName(attributes.getValue(i)); } } }else if (TAG_DOWN_URL.equals(localName)){ int length = attributes.getLength(); for (int i = 0 ; i < length ; i++){ String attrName = attributes.getLocalName(i); if (ATTR_APP_URL.equals(attrName)){ item.setAppUrl(attributes.getValue(i)); } } }else if (TAG_VERSION.equals(localName)){ int length = attributes.getLength(); for (int i = 0 ; i < length ; i++){ String attrName = attributes.getLocalName(i); if (ATTR_VER.equals(attrName)){ Log.i(TAG , " Version = " + attributes.getValue(i)); } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (TAG_APP.equals(localName)){ items.add(item); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { } }
还可以使用pull来生成XML
<pre name="code" class="java"> public static void createXml(List<Person> persons , OutputStream outputStream) throws Exception{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(outputStream, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "persons"); for (Person person : persons){ serializer.startTag(null, "person"); serializer.attribute(null, "id", "" + person.getId()); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(""+ person.getAge()); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); outputStream.flush(); outputStream.close(); }