简体   繁体   English

Android应用程序中的Facebook RSS Feed崩溃

[英]Facebook RSS Feed in Android app crashing

So I tried to set up an RSS feed for my Android application with the attached code, yet when I run it on the emulator, it crashes and I receive the attached errors in my LogCat. 因此,我尝试使用附带的代码为我的Android应用程序设置RSS feed,但是当我在模拟器上运行它时,它崩溃了,并且在LogCat中收到了附带的错误。 Any ideas for what could be wrong? 有什么想法可能有问题吗? Thanks in advance!!! 提前致谢!!!

package com.fixed_gear_app;

import java.io.IOException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
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;

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.activity_rssfeed);
        TextView rss = (TextView) findViewById(R.id.rss);
        try {
            URL rssUrl = new URL("https://www.facebook.com/feeds/page.php?format=rss20&id=619608694724497");
            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);

        } catch (IOException e) {rss.setText(e.getMessage());
        } catch (SAXException e) {rss.setText(e.getMessage());
        } catch (ParserConfigurationException e) {rss.setText(e.getMessage());
        }

        rss.setText(rssResult);
    }
    /**public String removeSpaces(String s) {
          StringTokenizer st = new StringTokenizer(s," ",false);
          String t="";
          while (st.hasMoreElements()) t += st.nextElement();
          return t;
        }*/
    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";

        }

    }
}

LogCat

You are attempting to do network communication on the main Activity thread. 您正在尝试在主“活动”线程上进行网络通信。 Android, by default, disallows this, (as you do not know how long it can take) so you will have to move your networking code to a class extending AsyncTask. 默认情况下,Android不允许这样做(因为您不知道要花多长时间),因此您必须将网络代码移至扩展AsyncTask的类。 Have a read of this , which shows you how to put it on an AsyncTask. 阅读此内容 ,它向您展示如何将其放在AsyncTask上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM