android将rss项加载到listview中

时间:2015-04-24 11:13:33

标签: android listview rss-reader

您好我会在我的MainActivity中将rss项加载到listview中。为此,我正在使用这个RSS-Reader-Library

  

https://github.com/matshofman/Android-RSS-Reader-Library

在他的描述中,开发人员解释了如何使用这些库,只需使用

URL url = new URL("http://example.com/feed.rss");
RssFeed feed = RssReader.read(url);

ArrayList<RssItem> rssItems = feed.getRssItems();
for(RssItem rssItem : rssItems) {
    Log.i("RSS Reader", rssItem.getTitle());
}

我试过但我的应用程序在开始时崩溃了。这是我的MainActivity:

public class MainActivity extends ActionBarActivity{

    private ListView listview;
    URL url = null;
    RssFeed feed = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.listView);

        try {
            url = new URL("http://www.unikore.it/index.php/ingegneria-informatica-home?format=feed");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            feed = RssReader.read(url);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ArrayList<RssItem> rssItems = (ArrayList<RssItem>)feed.getRssItems();
        ArrayAdapter<RssItem> list = new ArrayAdapter<RssItem>(getBaseContext(), android.R.layout.simple_list_item_1, rssItems);
        listview.setAdapter(list);
        for(RssItem rssItem : rssItems) {

            Log.i("RSS Reader", rssItem.getTitle());
        }
    }
}

这是logacat:

04-24 13:09:03.120  32415-32415/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asuspc.rssfeed/com.example.asuspc.rssfeed.MainActivity}: android.os.NetworkOnMainThreadException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
            at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
            at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
            at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
            at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
            at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
            at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
            at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
            at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
            at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
            at java.net.URL.openStream(URL.java:462)
            at com.example.asuspc.rssfeed.RssReader.read(RssReader.java:36)
            at com.example.asuspc.rssfeed.MainActivity.onCreate(MainActivity.java:46)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)

例外是:android.os.NetworkOnMainThreadException 你能解释一下如何加载rss项目并将每个人展示到我的列表视图中吗?

由于

1 个答案:

答案 0 :(得分:1)

尝试使用AsyncTask,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listview = (ListView) findViewById(R.id.listView);

    try {
        url = new URL(
                "http://www.unikore.it/index.php/ingegneria-informatica-home?format=feed");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    new ReadRssTask().execute(url);
}

private class ReadRssTask extends AsyncTask<URL, Void, RssFeed> {

    @Override
    protected RssFeed doInBackground(URL... params) {
        RssFeed result = null;
        URL url = params[0];
        if (!TextUtils.isEmpty(url)) {
            try {
                result = RssReader.read(url);
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    protected void onPostExecute(RssFeed result) {
        if (result != null) {
            ArrayList<RssItem> rssItems = (ArrayList<RssItem>) result
                    .getRssItems();
            List<String> titles = new ArrayList<String>();
            for (RssItem rssItem : rssItems) {
                titles.add(rssItem.getTitle());
            }
            ArrayAdapter<String> list = new ArrayAdapter<String>(
                    getBaseContext(), android.R.layout.simple_list_item_1,
                    titles);
            listview.setAdapter(list);
        }
    }

}

希望它有所帮助。