只读RSS Feed中的特定标签

时间:2013-04-15 14:19:43

标签: java android wordpress xml-parsing rss-reader

我正在尝试按照tutorial学习如何为我的Android应用程序制作RSS阅读器。 Feed是从wordpress博客生成的,我想找出一种按类别阅读的方法。它目前正在阅读整个Feed项目,但我正在尝试从列表中挑选出特定的类别。

这是阅读Feed的Activity类。

// Connected - Start parsing
        new AsyncLoadXMLFeed().execute();

    }

}

private void startLisActivity(RSSFeed feed) {

    Bundle bundle = new Bundle();
    bundle.putSerializable("feed", feed);

    // launch List activity
    Intent intent = new Intent(SplashActivity.this, GridActivity.class);
    intent.putExtras(bundle);
    startActivity(intent);

    // kill this activity
    finish();

}

private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        // Obtain feed
        DOMParser myParser = new DOMParser();
        feed = myParser.parseXml(http://mywordpressblog.com/feed/);
        if (feed != null && feed.getItemCount() > 0)
            WriteFeed(feed);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        startLisActivity(feed);
    }

}

// Method to write the feed to the File
private void WriteFeed(RSSFeed data) {

    FileOutputStream fOut = null;
    ObjectOutputStream osw = null;

    try {
        fOut = openFileOutput(fileName, MODE_PRIVATE);
        osw = new ObjectOutputStream(fOut);
        osw.writeObject(data);
        osw.flush();
    }

    catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

// Method to read the feed from the File
private RSSFeed ReadFeed(String fName) {

    FileInputStream fIn = null;
    ObjectInputStream isr = null;

    RSSFeed _feed = null;
    File feedFile = getBaseContext().getFileStreamPath(fileName);
    if (!feedFile.exists())
        return null;

    try {
        fIn = openFileInput(fName);
        isr = new ObjectInputStream(fIn);

        _feed = (RSSFeed) isr.readObject();
    }

    catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        try {
            fIn.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return _feed;

}

1 个答案:

答案 0 :(得分:0)

在将项目添加到列表之前,您可以像这样检查:

如果(item.getCategory()。包含( “categorytoshow”)){

将其添加到列表中,因为它包含您想要的类别

}

然后将其添加到列表