解析RSS源时出错

时间:2013-12-30 18:51:46

标签: java android rss

我每次都会遇到同样的错误。我现在已经工作了大约4个小时。 它用于RSS提要。

错误可能很简单,但我是初学者,所以请耐心等待。

我尝试添加StringBuffer,这是我开始收到错误的地方。如果我删除了StringBuffer,那么该应用仍会继续有效,但我更愿意使用它。

original code is from here

我的代码

主要Activity.java

public class MainActivity extends Activity {

    // A reference to the local object
    private MainActivity local;

     /**
    * This method creates main application view
    */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // Set view
         setContentView(R.layout.activity_main);

         // Set reference to this activity
         local = this;

         GetRSSDataTask task = new GetRSSDataTask();

         // Start download RSS task
         task.execute("http://cantstophiphop.com/feed/");

         // Debug the thread name
         Log.d("RssReader", Thread.currentThread().getName());
     }

     private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {

         @Override
         protected List<RssItem> doInBackground(String... urls) {

             // Debug the task thread name
             Log.d("RssReader", Thread.currentThread().getName());

             try {
                 // Create RSS reader
                 RssReader rssReader = new RssReader(urls[0]);

                 // Parse RSS, get items

             } catch (Exception e) {
                 Log.e(" RssReader", e.getMessage());
             }

             return null;
         }

         @Override
         protected void onPostExecute(List<RssItem> result) {

             // Get a ListView from main view
             ListView itcItems = (ListView) findViewById(R.id.listView1);

             // Create a list adapter
             ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>  (local,android.R.layout.simple_list_item_1, result);
             // Set list adapter for the ListView
             itcItems.setAdapter(adapter);

             // Set list view item click listener
             itcItems.setOnItemClickListener(new ListListener(result, local));
         }
     }  
}

RssParseHandler.java

public class RssParseHandler extends DefaultHandler {

    // List of items parsed
    private List<RssItem> rssItems;
    // We have a local reference to an object which is constructed while parser is working on an item tag
    // Used to reference item while parsing
    private RssItem currentItem;
    // We have two indicators which are used to differentiate whether a tag title or link is being processed by the parser
    // Parsing title indicator
    private boolean parsingTitle;
    // Parsing link indicator
    private boolean parsingLink;
    //private rssTitle;
    private StringBuffer rssTitle;

    public RssParseHandler() {
        rssItems = new ArrayList();

    }

    // We have an access method which returns a list of items that are read from the RSS feed. This method will be called when parsing is done.
    public List<RssItem> getItems() {
        return rssItems;
    }

     // The StartElement method creates an empty RssItem object when an item start tag is being processed. When a title or link tag are being processed appropriate indicators are set to true.
    @Override
    public void startElement(String uri, String localName, String qName, Attributes  attributes) throws SAXException {

        if ("item".equals(qName)) {
            currentItem = new RssItem();
        } else if ("title".equals(qName)) {
            parsingTitle = true;
            rssTitle = new StringBuffer(); //Add this line *****************************
        } else if ("link".equals(qName)) {
            parsingLink = true;
        }
    }

    // The EndElement method adds the  current RssItem to the list when a closing item tag is processed. It sets appropriate indicators to false -  when title and link closing tags are processed
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException{

        if ("item".equals(qName)) {
            rssItems.add(currentItem);
            currentItem = null;
        } else if ("title".equals(qName)) {
            parsingTitle = false;
            currentItem.setTitle(rssTitle.toString()); //Add this line *****************
        } else if ("link".equals(qName)) {
            parsingLink = false;
        }
     }

    // Characters method fills current RssItem object with data when title and link tag content is being processed
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

        if (parsingTitle) {
            if (currentItem != null)
                rssTitle.append(new String(ch, start, length)); //add This line ***************
            } else if (parsingLink) {
                if (currentItem != null) { 
                currentItem.setLink(new String(ch, start, length));
                parsingLink = false;
            }
        }
    }
}

logcat的

12-30 19:03:20.792: E/AndroidRuntime(13573): FATAL EXCEPTION: main
12-30 19:03:20.792: E/AndroidRuntime(13573): java.lang.NullPointerException
12-30 19:03:20.792: E/AndroidRuntime(13573):    at      android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at android.widget.ListView.setAdapter(ListView.java:480)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at com.dailyloudrss.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:78)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at com.dailyloudrss.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:1)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at android.os.AsyncTask.finish(AsyncTask.java:632)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at android.os.Looper.loop(Looper.java:136)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at android.app.ActivityThread.main(ActivityThread.java:5017)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at java.lang.reflect.Method.invokeNative(Native Method)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at java.lang.reflect.Method.invoke(Method.java:515)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-30 19:03:20.792: E/AndroidRuntime(13573):    at dalvik.system.NativeStart.main(Native Method) 

0 个答案:

没有答案