难以解析Android项目的rss feed

时间:2021-12-11 01:42:52

I'm trying to create an RSS feed parsing app, and I am new to android programming. I currently have a Main Activity, which prompts the user to enter an rss URL, and the maxRows of the rss feed requested. I'm using the ROME library for RSS feed parsing in Java. Below is my code:

我正在尝试创建一个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;
        }

    }

The keys "url" and "maxRows" are used to get the two entered values entered. However, this code does not work... Particularly, it fails at reader = new XmlReader(url.openConnection());... and I'm not sure why. The goal of this activity is simply to query the url provided, and output feed descriptions as subsequent textViews. Am I doing this correctly?

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

Here's the full stack trace:

这是完整的堆栈跟踪:

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 个解决方案

#1


5  

The fundamental error is

根本错误是

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

无法启动活动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):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)09-11 17:20:11.601:E / AndroidRuntime (1198):at

You're trying to access the network on the main thread. Looks like you're trying to create a new Thread, but it's accessing the UI on the main thread. This will cause trouble.

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

The Android training class RUnning in A Background Service includes a sample app that has a robust RSS feed parser. It uses an IntentService to do the read in the background and store the results in a content provider, which is the way you should do it.

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

#1


5  

The fundamental error is

根本错误是

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

无法启动活动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):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)09-11 17:20:11.601:E / AndroidRuntime (1198):at

You're trying to access the network on the main thread. Looks like you're trying to create a new Thread, but it's accessing the UI on the main thread. This will cause trouble.

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

The Android training class RUnning in A Background Service includes a sample app that has a robust RSS feed parser. It uses an IntentService to do the read in the background and store the results in a content provider, which is the way you should do it.

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