2012-07-01 60 views
1

我有關於xmlreader的問題。我可以運行它,網址是正確的,但它沒有運行startelement方法。所有返回的值都是null。我想知道爲什麼會發生這種情況和解決辦法。謝謝!xmlreader不運行startelement方法

package com.headfirstlabs.nasadailyimage; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.UnknownHostException; 
import java.util.jar.Attributes; 

import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 

import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 
import org.xml.sax.XMLReader; 
import org.xml.sax.helpers.DefaultHandler; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.StrictMode; 

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 { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

     StrictMode.setThreadPolicy(policy); 
    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(UnknownHostException e) 
    { 
     title="UnknownHostException"; 
    } 
    catch(IOException e) 
    { 
     title="IOException"; 
    } 
    catch(SAXException e) 
    { 
     title="SAXException"; 
    } 
    catch (Exception e) { 

     title="Exception"; 
     System.out.println(e); 
    } 
    } 

    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 (IOException ioe) { return null; } 
    } 

    public void startElement(String uri, String localName, String qName, 
    Attributes attributes) throws SAXException { 
     if (localName.equals("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; } 
     } 
    } 

    public void characters(char ch[], int start, int length) { 
    String chars = new String(ch).substring(start, start + length); 
    if (inUrl && url == null) { image = getBitmap(chars); } 
    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; } 
} 

回答

4

問題是這一行:

import java.util.jar.Attributes; 

應該

import org.xml.sax.Attributes; 

因爲你已經得到了錯誤類型的startElement第四個參數,你的方法沒有按」 t覆蓋startElement,DefaultHandler,所以DefaultHandler中的startElement的默認實現被稱爲inst你的方法的首要問題。

您可以使用@Override註釋來指示方法應該重寫超類中的方法。如果使用@Override批註的方法未覆蓋超類方法,則會出現編譯器錯誤。事實上,如果您將此註釋放在您的startElement方法中,您將會遇到這樣的錯誤。