难以解析Android项目的rss feed

时间:2013-09-11 21:10:49

标签: java android rss rome

我正在尝试创建一个RSS feed解析应用程序,我是android编程的新手。我目前有一个主活动,它会提示用户输入一个rss URL,以及所请求的rss feed的maxRows。我正在使用ROME库在Java中进行RSS feed解析。以下是我的代码:

package com.xetur.simplerss;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

    public class ListRowsActivity extends Activity {
        private URL url;
        private int maxRows;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_rows);
            readFeed();
            new Thread(new Runnable() {
                public void run() {
                    readFeed();
                }
            }).start();
        }

        @SuppressWarnings("deprecation")
        private void readFeed() {
            XmlReader reader = null;

            try {
                Bundle extras = getIntent().getExtras();
                url = new URL(extras.getString("url"));
                maxRows = (Integer) extras.get("maxRows");
                reader = new XmlReader(url.openConnection());

                SyndFeed feed = new SyndFeedInput().build(reader);
                List<SyndEntry> entries = feed.getEntries();
                Iterator<SyndEntry> iEntries = entries.iterator();

                LinearLayout layout = (LinearLayout)     findViewById(R.id.list_rows_layout);

                int i = 0;
                while (i < maxRows) {
                    if (iEntries.hasNext()) {
                        SyndEntry entry = (SyndEntry) iEntries.next();
                        SyndContent content = entry.getDescription();
                        if (content != null) {
                            i++;
                            String description = content.getValue();
                            System.out.println(description);
                            TextView entryView = new TextView(getApplicationContext());
                            entryView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                            entryView.setId((int) System.currentTimeMillis());
                            entryView.setText(description);
                            layout.addView(entryView);
                        }
                    }
                }
            } catch (IllegalArgumentException e) {
            } catch (FeedException e) {
            } catch (IOException e) {
            } finally {
                if (reader != null)
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
            }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.list_rows, menu);
            return true;
        }

    }

键“url”和“maxRows”用于输入输入的两个值。但是,这段代码不起作用......特别是,它在reader = new XmlReader(url.openConnection()); ...时失败了,我不知道为什么。此活动的目标只是查询提供的网址,并将输入描述输出为后续textView。我这样做了吗?

这是完整的堆栈跟踪:

09-11 17:20:11.481: W/dalvikvm(1198): threadid=1: thread exiting with uncaught exception (group=0x41465700)
09-11 17:20:11.601: E/AndroidRuntime(1198): FATAL EXCEPTION: main
09-11 17:20:11.601: E/AndroidRuntime(1198): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xetur.simplerss/com.xetur.simplerss.ListRowsActivity}: android.os.NetworkOnMainThreadException
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.os.Looper.loop(Looper.java:137)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at java.lang.reflect.Method.invoke(Method.java:525)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at dalvik.system.NativeStart.main(Native Method)
09-11 17:20:11.601: E/AndroidRuntime(1198): Caused by: android.os.NetworkOnMainThreadException
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at com.sun.syndication.io.XmlReader.<init>(XmlReader.java:237)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at com.xetur.simplerss.ListRowsActivity.readFeed(ListRowsActivity.java:46)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at com.xetur.simplerss.ListRowsActivity.onCreate(ListRowsActivity.java:30)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.Activity.performCreate(Activity.java:5133)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-11 17:20:11.601: E/AndroidRuntime(1198):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-11 17:20:11.601: E/AndroidRuntime(1198):     ... 11 more

1 个答案:

答案 0 :(得分:5)

基本错误是

  

无法启动活动ComponentInfo {com.xetur.simplerss / com.xetur.simplerss.ListRowsActivity}:android.os。 NetworkOnMainThreadException 09-11 17:20:11.601:E / AndroidRuntime(1198) ):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)09-11 17:20:11.601:E / AndroidRuntime(1198):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)09- 11 17:20:11.601:E / AndroidRuntime(1198):at

您正尝试在主线程上访问网络。看起来你正在尝试创建一个新线程,但它正在访问主线程上的UI。这会造成麻烦。

Android培训课程RUnning in A Background Service包含一个示例应用程序,该应用程序具有强大的RSS提要解析器。它使用IntentService在后台执行读取并将结果存储在内容提供程序中,这是您应该这样做的方式。