Android学习第十七天----从网络上获取xml
利用pull解析,将获取到的xml显示在页面中。
package com.will.entity; public class Books { private Integer bookId; private String bookName; private float bookPrice; public Books() { super(); } public Integer getBookId() { return bookId; } public void setBookId(Integer bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public float getBookPrice() { return bookPrice; } public void setBookPrice(float bookPrice) { this.bookPrice = bookPrice; } @Override public String toString() { return "Books [bookId=" + bookId + ", bookName=" + bookName + ", bookPrice=" + bookPrice + "]"; } }
创建对象的实体类,然后创建服务类,
package com.will.service; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; import com.will.entity.Books; public class XmlService_1 { public static List<Books> getList(String path) throws IOException, XmlPullParserException { InputStream is = null; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5 * 1000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == 200) { is = conn.getInputStream(); } return XmlParse(is); } public static List<Books> XmlParse(InputStream is) throws IOException, XmlPullParserException { List<Books> list = null; Books book = null; XmlPullParser xpp = Xml.newPullParser(); xpp.setInput(is, "utf-8"); int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<Books>(); break; case XmlPullParser.START_TAG: if ("book".equals(xpp.getName())) { book = new Books(); book.setBookId(Integer.parseInt(xpp.getAttributeValue(0))); } if ("name".equals(xpp.getName())) { book.setBookName(xpp.nextText()); } if ("price".equals(xpp.getName())) { book.setBookPrice(Float.parseFloat(xpp.nextText())); } break; case XmlPullParser.END_TAG: if ("book".equals(xpp.getName())) { list.add(book); } break; default: break; } eventType = xpp.next(); } is.close(); return list; } public static void saveXML(List<Books> list,OutputStream output) { XmlSerializer serializer = Xml.newSerializer(); try { serializer.setOutput(output, "utf-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "books"); for (Books book : list) { serializer.startTag(null, "book"); serializer.attribute(null, "id", book.getBookId().toString()); serializer.startTag(null, "name"); serializer.text(book.getBookName()); serializer.endTag(null, "name"); serializer.startTag(null, "price"); serializer.text(String.valueOf(book.getBookPrice())); serializer.endTag(null, "price"); serializer.endTag(null, "book"); } serializer.endTag(null, "books"); serializer.endDocument(); output.flush(); output.close(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
在main中
package com.example.httpxml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.xmlpull.v1.XmlPullParserException; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import com.will.entity.Books; import com.will.service.XmlService_1; public class MainActivity extends ListActivity { private List<Books> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { list = XmlService_1 .getList("http://192.168.1.20:8080/javawebxml/XMLServlet"); File file = new File(MainActivity.this.getFilesDir() .getAbsolutePath() + "/books.xml"); FileOutputStream fos = new FileOutputStream(file); XmlService_1.saveXML(list, fos); MainActivity.this.setListAdapter(new ArrayAdapter<Books>( MainActivity.this, android.R.layout.simple_list_item_1, list)); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } } }