简体   繁体   English

Android项目难于解析RSS Feed

[英]Difficulty parsing rss feed for Android project

I'm trying to create an RSS feed parsing app, and I am new to android programming. 我正在尝试创建一个RSS feed解析应用程序,而我对android编程还是陌生的。 I currently have a Main Activity, which prompts the user to enter an rss URL, and the maxRows of the rss feed requested. 我目前有一个Main Activity,它提示用户输入rss URL,并要求rss feed的maxRows。 I'm using the ROME library for RSS feed parsing in Java. 我正在使用ROME库在Java中解析RSS feed。 Below is my code: 下面是我的代码:

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. 键“ url”和“ maxRows”用于获取输入的两个输入值。 However, this code does not work... Particularly, it fails at reader = new XmlReader(url.openConnection());... and I'm not sure why. 但是,此代码不起作用...特别是,它在reader = new XmlReader(url.openConnection()); ...处失败,并且我不确定为什么。 The goal of this activity is simply to query the url provided, and output feed descriptions as subsequent textViews. 该活动的目标只是查询提供的url,并将提要描述输出为后续的textView。 Am I doing this correctly? 我这样做正确吗?

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

The fundamental error is 基本错误是

Unable to start activity ComponentInfo{com.xetur.simplerss/com.xetur.simplerss.ListRowsActivity}: android.os. 无法启动活动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 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

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. 看起来您正在尝试创建一个新的线程,但是它正在访问主线程上的UI。 This will cause trouble. 这会造成麻烦。

The Android training class RUnning in A Background Service includes a sample app that has a robust RSS feed parser. Android培训课程RUnning in A Background Service包含一个示例应用程序,该应用程序具有强大的RSS feed解析器。 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. 它使用IntentService在后台进行读取,并将结果存储在内容提供程序中,这是您应该这样做的方式。

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

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