使用RSS解析库,不起作用。应用程序崩溃

时间:2013-11-20 02:56:31

标签: android rss sax saxparser

我正在尝试使用这个RSS parsing library实现非常简单的RSS解析器。根据实现,我只需粘贴提供的示例代码,一旦我将代码作为库导入。

我采取的步骤:

  1. 从上面提供的链接下载zip文件。
  2. 从现有代码创建新项目
  3. 将添加的项目作为库添加到我的项目中。
  4. 从上面提供的链接添加示例代码,并使用try和catch语句对其进行修改,并添加textview作为rss feed中标题的输出。
  5. 尝试并运行该应用程序。它崩溃了。
  6. 是因为图书馆很旧吗?我做错了什么吗?非常感谢任何帮助。


    代码:

    package com.example.rssparser;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    
    import org.xml.sax.SAXException;
    
    import nl.matshofman.saxrssreader.RssFeed;
    import nl.matshofman.saxrssreader.RssItem;
    import nl.matshofman.saxrssreader.RssReader;
    import android.app.Activity;
    import android.os.Bundle;
    //import android.util.Log;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    TextView rss_feed_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        URL url;
        try {
            url = new URL("http://wwwnc.cdc.gov/travel/rss/notices.xml");
    
            RssFeed feed;
            try {
                feed = RssReader.read(url);
                ArrayList<RssItem> rssItems = feed.getRssItems();
                for(RssItem rssItem : rssItems) {
                    //Log.i("RSS Reader", rssItem.getTitle());
                    rss_feed_data = (TextView) findViewById(R.id.rss_feed_data);
                    rss_feed_data.setText(rssItem.getTitle());
    
                }
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    @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;
       }
    
    }
    

    LOGCAT OUTPUT:

    11-20 04:01:36.901: E/Trace(16331): error opening trace file: No such file or directory (2)
    11-20 04:01:37.088: W/dalvikvm(16331): threadid=1: thread exiting with uncaught   exception (group=0x41269908)
    11-20 04:01:37.138: E/AndroidRuntime(16331): FATAL EXCEPTION: main
    11-20 04:01:37.138: E/AndroidRuntime(16331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rssparser/com.example.rssparser.MainActivity}:  android.os.NetworkOnMainThreadException
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2234)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.access$600(ActivityThread.java:181)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.os.Handler.dispatchMessage(Handler.java:99)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.os.Looper.loop(Looper.java:153)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.main(ActivityThread.java:5037)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.lang.reflect.Method.invokeNative(Native Method)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.lang.reflect.Method.invoke(Method.java:511)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:588)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at dalvik.system.NativeStart.main(Native Method)
    11-20 04:01:37.138: E/AndroidRuntime(16331): Caused by: android.os.NetworkOnMainThreadException
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:185)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at    java.net.URL.openStream(URL.java:462)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at nl.matshofman.saxrssreader.RssReader.read(RssReader.java:35)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at com.example.rssparser.MainActivity.onCreate(MainActivity.java:29)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.Activity.performCreate(Activity.java:5020)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
    11-20 04:01:37.138: E/AndroidRuntime(16331):    ... 11 more
    

    更新代码

    package com.example.rssparser;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    
    import nl.matshofman.saxrssreader.RssFeed;
    import nl.matshofman.saxrssreader.RssItem;
    import nl.matshofman.saxrssreader.RssReader;
    
    import org.xml.sax.SAXException;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.os.Build;
    import android.os.Bundle;
    import android.widget.TextView;
    //import android.util.Log;
    
    public class MainActivity extends Activity {
    TextView rss_feed_data;
    String ur = "http://wwwnc.cdc.gov/travel/rss/notices.xml";
    ProgressDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rss_feed_data = (TextView) findViewById(R.id.rss_feed_data);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
         new XmlParsing(ur).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null});
    else
         new XmlParsing(ur).execute(new String[]{null});
    
    }
    
    public class XmlParsing extends AsyncTask<String, String, ArrayList<RssItem>> {
    
        // variables passed in:
        String urls;
        //  constructor
        public XmlParsing(String urls) {
            this.urls = urls;
        }
    
        @Override
        protected void onPreExecute() {
            pDialog = ProgressDialog.show(MainActivity.this, "Fetching Details..", "Please wait...", true);
        }
    
    
        @Override
        protected ArrayList<RssItem> doInBackground(String... params) {
            // TODO Auto-generated method stub
    
    
            URL url;
            try {
                url = new URL(urls);
                RssFeed feed;
                try {
                    feed = RssReader.read(url);
                    ArrayList<RssItem> rssItems = feed.getRssItems();
                    return rssItems;
                    } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace(); }
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                return null;
        }
    
        protected void onPostExecute(String result, ArrayList<RssItem> rssItems) {
            // Now we have your JSONObject, play around with it.
            if (pDialog.isShowing())
                pDialog.dismiss();
    
            for(RssItem rssItem : rssItems) {
                    //Log.i("RSS Reader", rssItem.getTitle());
    
                    rss_feed_data.setText(rssItem.getTitle());
            }
    
        }
    }
    }
    

    LOGCAT输出

    11-20 05:36:50.439: I/SurfaceTextureClient(17281): [STC::queueBuffer] this:0x516a20a8, api:1, last queue time elapsed :9510 ms
    11-20 05:36:50.441: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:10.505000, fps:6.282219
    11-20 05:36:50.473: I/SurfaceTextureClient(17281): [STC::queueBuffer] this:0x51655180, api:1, last queue time elapsed :131816 ms
    11-20 05:36:50.494: I/SurfaceTextureClient(17281): [0x51655180] frames:1, duration:131.815994, fps:0.007586
    11-20 05:36:51.426: I/SurfaceTextureClient(17281): [0x516a20a8] frames:45, duration:1.016000, fps:44.289616
    11-20 05:36:52.437: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.308807
    11-20 05:36:53.449: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.186592
    11-20 05:36:54.462: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.197708
    11-20 05:36:55.474: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.203377
    11-20 05:36:56.486: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.198250
    11-20 05:36:57.498: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.211411
    11-20 05:36:58.509: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.283676
    11-20 05:36:59.520: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66,  duration:1.010000, fps:65.318069
    11-20 05:37:00.530: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.322647
    11-20 05:37:01.542: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.240547
    11-20 05:37:02.556: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.244469
    11-20 05:37:03.565: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.245308
    11-20 05:37:04.577: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.252708
    11-20 05:37:05.588: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.248543
    11-20 05:37:06.600: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.246178
    11-20 05:37:07.611: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.253662
    11-20 05:37:08.622: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.253265
    11-20 05:37:09.634: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.248100
    11-20 05:37:10.646: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.222153
    11-20 05:37:11.656: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.313301
    11-20 05:37:12.667: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.318176
    11-20 05:37:13.678: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.267540
    11-20 05:37:14.689: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.262505
    11-20 05:37:15.701: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.258896
    11-20 05:37:16.712: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.275986
    11-20 05:37:17.723: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.265404
    11-20 05:37:18.734: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.277664
    11-20 05:37:19.745: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.250298
    

1 个答案:

答案 0 :(得分:1)

当应用程序尝试在其主线程上执行网络操作时,会抛出此异常。在AsyncTask中运行您的代码:

public class MainActivity extends Activity {
TextView rss_feed_data;
String ur = "http://wwwnc.cdc.gov/travel/rss/notices.xml";
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rss_feed_data = (TextView) findViewById(R.id.rss_feed_data);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
         new XmlParsing(ur).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null});
    else
         new XmlParsing(ur).execute(new String[]{null});

}

public class XmlParsing extends AsyncTask<String, String, ArrayList<RssItem>> {

        // variables passed in:
        String urls;
        //  constructor
        public XmlParsing(String urls) {
            this.urls = urls;
        }

        @Override
        protected void onPreExecute() {
            pDialog = ProgressDialog.show(NetActivity.this, "Fetching Details..", "Please wait...", true);
        }


        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub


            URL url;
        try {
            url = new URL();

            RssFeed feed;
            try {
                feed = RssReader.read(url);
                ArrayList<RssItem> rssItems = feed.getRssItems();
                return rssItems;
                }
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // Now we have your JSONObject, play around with it.
            if (pDialog.isShowing())
                pDialog.dismiss();

            for(RssItem rssItem : rssItems) {
                    //Log.i("RSS Reader", rssItem.getTitle());

                    rss_feed_data.setText(rssItem.getTitle());
        }

    }