android—pull解析7111337271

來源:互聯網
上載者:User

 

 
 
-------------------------------------------------------------------------------domain
 
package com.sforus.TextShangJi.domain;public class Book {private String title;private String author;private String isbn13;private String publisher;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getIsbn13() {return isbn13;}public void setIsbn13(String isbn13) {this.isbn13 = isbn13;}public String getPublisher() {return publisher;}public void setPublisher(String publisher) {this.publisher = publisher;}}

 
 
 
 
 
---------------------------------------------------------------
下面是處理xml
 
--------------------------------------------------------------
package com.sforus.TextShangJi.util;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import com.sforus.TextShangJi.domain.Book;import android.content.Context;import android.util.Xml;import com.google.gdata.client.douban.DoubanService;import net.htmlparser.jericho.Element;import net.htmlparser.jericho.Source;/** * 訪問資料庫,擷取書本 *  * @author woops *  */public class ConnectDataBase {private Book book;public List<Book> getNewBooks() throws Exception { List<Book> books = null;   String path = "http://api.douban.com/book/subjects?tag=android&start-index=1&max-results=5";URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);InputStream inStream = conn.getInputStream();XmlPullParser parser = Xml.newPullParser();parser.setInput(inStream, "UTF-8");int eventType = parser.getEventType();// 產生第一個事件while (eventType != XmlPullParser.END_DOCUMENT) {boolean s=false;switch (eventType) {case XmlPullParser.START_DOCUMENT:books = new ArrayList<Book>();break;

case XmlPullParser.START_TAG:    if ("entry".equals(parser.getName())) {      book = new Book();          }    if (book!=null) {     if ("title".equals(parser.getName())) {      String title=parser.nextText().toString();      book.setTitle(title);     }     if ("name".equals(parser.getName())){

      String name=parser.nextText().toString();      book.setAuthor(name);     }     if ("name".equals(parser.getName()==null)) {      String name=null;      book.setAuthor(name);     }     if("attribute".equals(parser.getName())){           if ("isbn13".equals(parser.getAttributeValue(0))) {        book.setIsbn13( parser.nextText());            }else if ("publisher".equals(parser.getAttributeValue(0))) {             book.setPublisher( parser.nextText());      }      }    }    break;   case XmlPullParser.END_TAG:    if ("entry".equals(parser.getName())&& eventType == XmlPullParser.END_TAG ) {     books.add(book);     book=null;    }    break;   }   eventType=parser.next();  }  return books;

}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.