开发者

cannot Override startElement(String, String, String, Attributes)

I'm trying to override

    startElement(String, String, String, Attributes)

But I got this message:

    The method startElement(String, String, String, Attributes) of type AlWesal.RSSHandler must override or implement a supertype method

This is my code:

    @Override
    public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        if (localName.equalsIgnoreCase("title"))
        {
            state = stateTitle;
            strElement = "Title: ";
            numberOfTitle++;
        }
        else
        {
            state = stateUnknown;
        }
    }

This is my entire class:

package com.exercise.AndroidRssReader;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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

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 android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidRssReader extends Activity {

    String streamTitle = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView result = (TextView)findViewById(R.id.result);

        try {
            URL rssUrl = new URL("http://feeds.feedburner.com/Android-er?format=xml");
            SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
            SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
            XMLReader myXMLReader = mySAXParser.getXMLReader();
            RSSHandler myRSSHandler = new RSSHandler();
            myXMLReader.setContentHandler(myRSSHandler);
            InputSource myInputSource = new InputSource(rssUrl.openStream());
            myXMLReader.parse(myInputSource);

            result.setText(streamTitle);

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setText("Cannot connect RSS!");
        } catch (ParserConfigurationException e) {
            // TODO Auto-genera开发者_运维百科ted catch block
            e.printStackTrace();
            result.setText("Cannot connect RSS!");
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setText("Cannot connect RSS!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setText("Cannot connect RSS!");
        }


    }

    private class RSSHandler extends DefaultHandler
    {
        final int stateUnknown = 0;
        final int stateTitle = 1;
        int state = stateUnknown;

        int numberOfTitle = 0;
        String strTitle = "";
        String strElement = "";

        @Override
        public void startDocument() throws SAXException {
            // TODO Auto-generated method stub
            strTitle = "--- Start Document ---\n";
        }

        @Override
        public void endDocument() throws SAXException {
            // TODO Auto-generated method stub
            strTitle += "--- End Document ---";
            streamTitle = "Number Of Title: " + String.valueOf(numberOfTitle) + "\n"
                        + strTitle;
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            if (localName.equalsIgnoreCase("title"))
            {
                state = stateTitle;
                strElement = "Title: ";
                numberOfTitle++;
            }
            else
            {
                state = stateUnknown;
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            // TODO Auto-generated method stub
            if (localName.equalsIgnoreCase("title"))
            {
                strTitle += strElement + "\n";
            }
            state = stateUnknown;
        }

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            // TODO Auto-generated method stub
            String strCharacters = new String(ch, start, length);
            if (state == stateTitle)
            {
                strElement += strCharacters;
            }
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜