一、JSON对象解析
JSON是一种轻量级的对象,数据体积小,方便传输,易于解析!
首先新建一个类工具类JsonUtil,用于获取请求返回的数据
public class JsonUtil { private static final String TAG = "JSONUTIL"; public static JSONObject getJSON(String url) throws Exception { return new JSONObject(getRequest(url)); } protected static String getRequest(String url) { return getRequest(url, new DefaultHttpClient(new BasicHttpParams())); } protected static String getRequest(String url, DefaultHttpClient client) { String result = null; int statusCode = 0; HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = client.execute(httpGet); statusCode = httpResponse.getStatusLine().getStatusCode();// statusCode为200时表示请求数据成功 result = parseInputStream(httpResponse.getEntity()); } catch (IOException e) { e.printStackTrace(); } finally { httpGet.abort(); } return result; } private static String parseInputStream(HttpEntity entity) { StringBuilder sb = null; try { sb = new StringBuilder(""); InputStream inputStream = entity.getContent(); int length = 0; byte[] buffer = new byte[1024]; while ((length = inputStream.read(buffer)) > -1) { sb.append(new String(buffer, 0, length)); } return sb.toString(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } }
获取数据并解析数据:注:模拟器访问自己电脑上的网站不能用localhost:8080或者127.0.0.1:8080,因为模拟器默认将模拟器本身设定为localhost,所以如果设置为这样的方式就将访问模拟器本身。我们需要将主机名修改为10.0.2.2,此主机名是模拟器设定的特定的访问自己电脑的主机名,它记录了你的电脑的名称。另外:获取数据需要将下面的方法封装到一个新线程中,不能放在程序主线程当中!
/* http://10.0.2.2:8080/index.jsp * { students:[{name:'Livingstone',age:25},{name:'LS',age:26}], class:'09GIS' } */ private void Livingstone() { try { String URL = "http://10.0.2.2:8080/index.jsp"; // 获取后台返回的JSON对象 --> { students:[{name:'Livingstone',age:25},{name:'LS',age:26}],class:'09GIS班' } JSONObject jObj = JsonUtil.getJSON(URL); // 获取学生数组 --> students:[{name:'Livingstone',age:25},{name:'LS',age:26}] JSONArray jArr = jObj.getJSONArray("students"); // 获取班级 --> class:'09GIS班' String classname = jObj.getString("class"); // 根据索引获取第一个学生的JSON对象 --> {name:'Livingstone',age:25} JSONObject j1 = jArr.getJSONObject(0); String studentInfo = jArr.length() + "个学生" + j1.getString("name") + j1.getInt("age"); } catch (Exception e) { e.printStackTrace(); } }
二、XML数据对象解析与保存
在Android平台上可以使用SAX、DOM和Android附带的pull解析器解析XML文件;
pull解析器提供了各种事件,使用parser.next()方法进入下一个元素并触发相应事件,pull解析器产生的事件是一个数字,可以通过switch对感兴趣的事件进行处理;通过paser.nextText()方法可以获取下一个Text类型的节点的值;
http://xmlpull.org/xmlpull-website/impls.shtml;
>>1.读取XML
XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(xml, "UTF-8");//为破解器添加要解析的XML数据 int event = pullParser.getEventType();//开始读取,获取事件返回值 pullParser.getName();//获取节点名称 pullParser.getAttributeValue(0);//获取第一个属性的值 pullParser.nextText();//获取标签之后的节点的内容 event = pullParser.next();//解析器遇到结束标签不会自动向下解析,需要调用此方法进行继续执行;
>>2.保存文件到XML
public static void save(List<Person> persons, OutputStream outStream)throws Exception, IOException { XmlSerializer serializer = Xml.newSerializer();//获取XML写入信息的序列化对象 serializer.setOutput(outStream, "UTF-8");//设置要写入的OutputStream serializer.startDocument("UTF-8", true);//设置文档标签 serializer.startTag(null, "persons");//设置开始标签,第一个参数为namespace for (Person person : persons) { serializer.startTag(null, "person"); serializer.attribute(null, "id", person.getId().toString()); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(person.getAge().toString()); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); outStream.flush(); outStream.close(); }