Android Open Source - RssSample Rss Parser Task Loader






From Project

Back to project page RssSample.

License

The source code is released under:

Apache License

If you think the Android project RssSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sample.rsssample;
/*from   w  ww  .ja v  a 2 s  .  c o  m*/
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Xml;

public class RssParserTaskLoader extends AsyncTaskLoader<List<Item>> {
    private String url;

    public RssParserTaskLoader(Context context) {
        super(context);
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public List<Item> loadInBackground() {
        List<Item> result = null;
        try {
            // HTTP??????????????InputStream?????????
            URL url = new URL(this.url);
            InputStream is = url.openConnection().getInputStream();
            result = parseXml(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // ??????????????????????onPostExecute????????????????????????
        return result;
    }

    // XML????????
    public List<Item> parseXml(InputStream is) throws IOException, XmlPullParserException {
        XmlPullParser parser = Xml.newPullParser();
        List<Item> list = new ArrayList<Item>();

        try {
            parser.setInput(is, null);
            int eventType = parser.getEventType();
            Item currentItem = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tag = null;
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    tag = parser.getName();
                    if (tag.equals("item")) {
                        currentItem = new Item();
                    } else if (currentItem != null) {
                        if (tag.equals("title")) {
                            currentItem.setTitle(parser.nextText());
                        } else if (tag.equals("description")) {
                            currentItem.setDescription(parser.nextText());
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    tag = parser.getName();
                    if (tag.equals("item")) {
                        list.add(currentItem);
                    }
                    break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}




Java Source Code List

com.sample.rsssample.ItemDetailActivity.java
com.sample.rsssample.Item.java
com.sample.rsssample.MainActivity.java
com.sample.rsssample.RssListAdapter.java
com.sample.rsssample.RssParserTaskLoader.java