xml与json
发布时间:2020-12-16 05:59:47 所属栏目:百科 来源:网络整理
导读:package com.itheima.xml;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {private PersonService service;@Overrideprotected void onCreate(Bundle s
package com.itheima.xml; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { private PersonService service; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); service = new PersonService(); } public void getJson(View v) { // AsyncHttpClient改造 new Thread() { public void run() { try { List<Person> list = service.getJsonPersons(); for (Person p : list) { System.out.println(p); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } public void getXml(View v) throws Exception { new Thread() { public void run() { try { List<Person> list = service.getXmlPersons(); for (Person p : list) { System.out.println(p); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
package com.itheima.xml; public class Person { private Long id; private String name; private Integer age; public Person() { super(); } public Person(Long id,String name,Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ",name=" + name + ",age=" + age + "]"; } } package com.itheima.xml; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; public class PersonService { public List<Person> getXmlPersons() throws Exception { URL url = new URL("http://192.168.1.228:8080/05.Web/persons.xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { InputStream in = conn.getInputStream(); return parseXml(in); } throw new RuntimeException("网络出错: " + code); } private List<Person> parseXml(InputStream in) throws Exception { List<Person> list = new ArrayList<Person>(); Person p = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(in,"UTF-8"); for (int type = parser.getEventType(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.START_TAG) { if ("person".equals(parser.getName())) { p = new Person(); String id = parser.getAttributeValue(0); p.setId(Long.parseLong(id)); list.add(p); } else if ("name".equals(parser.getName())) { String name = parser.nextText(); p.setName(name); } else if ("age".equals(parser.getName())) { String age = parser.nextText(); p.setAge(Integer.parseInt(age)); } } } return list; } public List<Person> getJsonPersons() throws Exception { URL url = new URL("http://192.168.1.228:8080/05.Web/persons.js"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int length; while((length = in.read(buffer)) != -1) // 从网络读取数据 out.write(buffer,length); // 把数据写到内存 in.close(); out.close(); byte[] data = out.toByteArray(); String json = new String(data); return parseJson(json); } return null; } private List<Person> parseJson(String json) throws Exception { List<Person> list = new ArrayList<Person>(); JSONArray arr = new JSONArray(json); // 把字符串封装成JSON数组 for (int i = 0; i < arr.length(); i++) { // 遍历JSON数组 JSONObject obj = arr.getJSONObject(i); // 从JSON数组中获取每个JSON对象 Person p = new Person(); p.setId(obj.getLong("id")); // 从JSON对象中获取id属性 p.setName(obj.getString("name")); p.setAge(obj.getInt("age")); list.add(p); } return list; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |