忍者ブログ

アンドロイドのあれこれ

XmlPullParserでRSSを取得する
AndroidでRSSの情報を取得するサンプルを紹介します。

AsyncTaskを使って非同期処理でRSSを取得してXmlPullParserでパースしMainActivityに返す流れです。
// RssParserAsyncTask.java
public class RssParserAsyncTask extends AsyncTask<String, Integer, ArrayList<NewsItem>>
{
    private NewsActivity mActivity = null;
    
    private String xmldataString = null;

    public RssParserAsyncTask(NewsActivity activity) {
        mActivity = activity;
    }

    @Override
    protected void onPreExecute(){
    }

    //バックグラウンドにおける処理
    @Override
    protected ArrayList<NewsItem> doInBackground(String... params){
        ArrayList<NewsItem> result = null;
        try {
            URL url = new URL(params[0]);
            InputStream is = url.openConnection().getInputStream();
            result = parseXml(is);
        } catch (Exception e) {
            Log.v("ERROR", "on doInBackground msg:" + e.getMessage());
            return null;
        }
        return result;
    }

    @Override
    protected void onPostExecute (ArrayList<NewsItem> result){
        mActivity.setListNews(result);
    }

    // XMLをパースする
    public ArrayList<NewsItem> parseXml(InputStream is)
        throws IOException, XmlPullParserException
    {
        ArrayList<NewsItem> itemList = new ArrayList<NewsItem>();
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(is, null);
            int eventType = parser.getEventType();
            NewsItem currentItem = null;
            int itemCount = 0;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tag = null;
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    tag = parser.getName();
                    if (tag.equals("item")) {
                        currentItem = new NewsItem();
                    } else if (currentItem != null) {
                        if (tag.equals("title")) {
                            currentItem.setTitle(parser.nextText());
                        } else if (tag.equals("description")) {
                            currentItem.setText(parser.nextText());
                        } else if (tag.equals("link")) {
                            currentItem.setLink(parser.nextText());
                        }

                    }
                    break;
                case XmlPullParser.END_TAG:
                    tag = parser.getName();
                    if (tag.equals("item")) {
                        itemList.add(currentItem);
                        itemCount++;
                    }
                    break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            Log.v("ERROR", "on parseXML msg:" + e.getMessage());
            return null;
        }
        return itemList;
    }
}
//NewsItem.xml
public class NewsItem
{
    private String _Title;
    private String _Text;
    private String _Link;
    
    
    public void setTitle(String title) {
        _Title = title;
    }
    
    public String getTitle() {
        return _Title;
    }

    public void setText(String text) {
        _Text = text;
    }
    
    public String getText() {
        return _Text;
    }
    
    public void setLink(String link) {
        _Link = link;
    }
    
    public String getLink() {
        return _Link;
    }
}
//MainActivity.java
public class MainActivity extends ListActivity
{
    private ArrayList<NewsItem> nList;
    private NewsListAdapter adapter;
    private String rssUrl = "..."; //RSSのURL

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_list);

        RssParserAsyncTask rssTask = new RssParserAsyncTask(this);
        rssTask.execute(rssUrl);
    }
    
    public void setListNews(ArrayList<NewsItem> listItem) {
        nList = new ArrayList();
        if (listItem != null) {
            int itemNum = listItem.size();
            for (int a=0; a<itemNum; a++) {
                nList.add(listItem.get(a));
            }
        }
        adapter = new NewsListAdapter(this, nList);
        setListAdapter(adapter);
    }

COMMENT
NAME
TITLE
MAIL (非公開)
URL
EMOJI
Vodafone絵文字 i-mode絵文字 Ezweb絵文字
COMMENT
PASS (コメント編集に必須です)
SECRET
管理人のみ閲覧できます
 
PR
© Android Advent
powered by 忍者ツールズ / 忍者ブログ / [PR]