V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
brikhoff
V2EX  ›  问与答

求v友帮测一下这段代码

  •  
  •   brikhoff · 2013-08-03 02:22:55 +08:00 · 2247 次点击
    这是一个创建于 4224 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码:
    package com.hardcorelab.nasaimageoftheday;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    /**
    * Created by hulk on 13-7-30.
    */
    public class IotdHandler extends DefaultHandler {
    private String url = "http://www.nasa.gov/rss/image_of_the_day.rss";
    private boolean inUrl = false;
    private boolean inTitle = false;
    private boolean inDescription = false;
    private boolean inItem = false;
    private boolean inDate = false;
    private Bitmap image = null;
    private String title = null;
    private StringBuffer description = new StringBuffer();
    private String date = null;
    public void processFeed() {
    try {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setContentHandler(this);
    InputStream inputStream = new URL(url).openStream();
    reader.parse(new InputSource(inputStream));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    private Bitmap getBitmap(String url){
    try {
    HttpURLConnection connection=(HttpURLConnection)new URL(url).openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input=connection.getInputStream();
    Bitmap bitmap= BitmapFactory.decodeStream(input);
    input.close();
    return bitmap;
    } catch (Exception e) {
    return null;
    }
    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    System.out.println("In--------------->"+localName);
    System.out.println("Start------>"+localName);
    if (localName.equals("enclosure")) {
    url=attributes.getValue("url");
    inUrl=true;
    } else {
    inUrl=false;
    }
    if (localName.startsWith("item")) {
    inItem=true;
    } else {
    if (inItem) {
    if (localName.equals("title")) {
    inTitle=true;
    } else {
    inTitle=false;
    }
    if (localName.equals("description")) {
    inDescription=true;
    } else {
    inDescription=false;
    }
    if (localName.equals("pubDate")) {
    inDate=true;
    } else {
    inDate=false;
    }
    }
    }
    }
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
    String chars = new String(ch).substring(start, start + length);
    System.out.println(chars);
    if (inUrl) {
    image = getBitmap(url);
    }
    if (inTitle && title == null) {
    title = chars;
    }
    if (inDescription) {
    description.append(chars);
    }
    if (inDate && date==null) {
    date=chars;
    }
    }
    public Bitmap getImage() {
    return image;
    }
    public String getTitle() {
    return title;
    }
    public StringBuffer getDescription() {
    return description;
    }
    public String getDate() {
    return date;
    }
    }
    view raw IotdHander.java hosted with ❤ by GitHub

    header first android development的第一个栗子我就阵亡了。
    运行processFeed()死活取不盗localName,一个都不输出。
    折腾一个星期了。。。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3149 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:33 · PVG 21:33 · LAX 05:33 · JFK 08:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.