0
package android.rss;

import android.app.Activity;
import android.os.Bundle;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.view.Menu;
import android.widget.TextView;
import java.net.URL;
import java.io.IOException;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;

public class RSSFeed extends Activity {
    /** Called when the activity is first created. */
    String rssResult = "";
    boolean item = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView rss = (TextView) findViewById(R.id.rss);
        try {
            URL rssUrl = new URL("http://rfs.ru/rss/news.rss");
            // http://feeds2.feedburner.com/Mobilab
            //http://go-football.org/news/rss.xml
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();            
            RSSHandler rssHandler = new RSSHandler();
            xmlReader.setContentHandler(rssHandler);
            InputSource inputSource = new InputSource(rssUrl.openStream());
            xmlReader.parse(inputSource);
            rss.setText(rssResult);
        } catch (IOException e) {rss.setText(e.getMessage());
        } catch (SAXException e) {rss.setText(e.getMessage());
        } catch (ParserConfigurationException e) {rss.setText(e.getMessage());
        }
    }

    private class RSSHandler extends DefaultHandler {

        public void startElement(String uri, String localName, String qName, 
Attributes attrs) throws SAXException {
            if (localName.equals("item"))
                item = true;

            if (!localName.equals("item") && item == true)
                rssResult = rssResult + localName + ": " ;

        }

        public void endElement(String namespaceURI, String localName, 
String qName) throws SAXException {

        }

        public void characters(char[] ch, int start, int length)
                throws SAXException {
            String cdata = new String(ch, start, length);
            if (item == true)
                rssResult = rssResult +(cdata.trim()).replaceAll("\\s+",  " ")+"\t";

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

много тегов . хотелось видет текс и заголовок.без технич инф.

2
  • 1
    а вопрос собственно в чем?
    – KoVadim
    11 июн 2013 в 12:58
  • все работает.я знаю что не висло нужно все в отдельном потоке.вопрос не в это это я сделаю.как избавиться от лишних тегов и техн инф в отображаемом тексте ленте
    – Радик
    11 июн 2013 в 13:13

1 ответ 1

5

Вопрос наверняка в том, почему не работает.

Ответ: потому что нельзя в основном потоке работать с интернетом

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.