0

I can parse data successfully from an RSS feed and list all the titles/dates... etc. in a listview.

However, I want to insert all the retrieved data (from the RSS feed) to a database where I can display them when needed.

I have been stuck with the this problem for a couple of hours now and the error I keep getting is AsyncTask #1

The following is my code;

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

        listview = (ListView)findViewById(R.id.rssChannelListView);
        //text = (TextView)findViewById(R.id.TextView01);

        mySQLiteAdapter = new DataAdapter(this);
        mySQLiteAdapter.createDatabase();
        mySQLiteAdapter.open();


        // Get the RSS URL that was set in the RssTabActivity
        String rssUrl = (String)getIntent().getExtras().get("rss-url");

        GetRSSDataTask task = new GetRSSDataTask();

        // Start process RSS task
        task.execute(rssUrl);

    }

    /**
     * This class downloads and parses RSS Channel feed.
     */
    private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
        @Override
        protected List<RssItem> doInBackground(String... urls) {
            try {   
                // Create RSS reader
                RssReader rssReader = new RssReader(urls[0]);
                saveContact();
                // Parse RSS, get items
                return rssReader.getItems();

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

            return null;
        }

         protected void onPreExecute(){
             super.onPreExecute();
             dialog = ProgressDialog.show(RssChannelActivity.this,"", "loading...");
         }
        @Override
        protected void onPostExecute(List<RssItem> result) {

            // Get a ListView from the RSS Channel view
            ListView itcItems = (ListView) findViewById(R.id.rssChannelListView);
            // Create a list adapter
            ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(RssChannelActivity.this,
                    android.R.layout.simple_list_item_1, result);
            // Set list adapter for the ListView
            itcItems.setAdapter(adapter);
            // Dismiss dialog
            dialog.dismiss();

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


     private void saveContact() 
     {
         DataAdapter dbConnector = new DataAdapter(this);
         dbConnector.insertTitleDate(dbcurrentItem.getIDDB(),
                 dbcurrentItem.getTitleDB().toString(),
                 dbcurrentItem.getPubDateDB().toString());
     }
}

EDIT:

my Handler Class:

public class RssParseHandler extends DefaultHandler {

    DataAdapter dbConnector;    

    // Used to reference items in db
    private DBItem dbcurrentItem;

    private List<RssItem> rssItems;

    // Used to reference item while parsing
    private RssItem currentItem;

    // Parsing title indicator
    private boolean parsingTitle;

    // Parsing pubdate indicator
    private boolean parsingDescription; 

    // Parsing pubdate indicator
    private boolean parsingPubDate; 

    // Parsing link indicator
    private boolean parsingLink;

    // A buffer used to build current title being parsed
    private StringBuffer currentSb;


    public RssParseHandler() {
        rssItems = new ArrayList<RssItem>();
    }

    public List<RssItem> getItems() {
        return rssItems;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("item".equals(qName)) {
            currentItem = new RssItem();
            currentSb = new StringBuffer();
        } 
        else if ("title".equals(qName)) {
            parsingTitle = true;            
        } 
        else if ("description".equals(qName)) {
            parsingDescription = true;          
        }
        else if ("pubDate".equals(qName)) {
            parsingPubDate = true;          
        }
        else if ("link".equals(qName)) {
            parsingLink = true;
        }
    }

    @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;           
            // Set item's title when we parse item->title tag not the channel title tag
            if (currentItem != null) {
                // Set item's title here
                /* if this is deleted it will NOT print on the listview */
                currentItem.setTitle(currentSb.toString());
            }           
        }
        else if ("description".equals(qName)) {         
            parsingDescription = false;         
            // Set item's title when we parse item->title tag not the channel title tag
            if (currentItem != null) {
                // Set item's title here
                /* if this is deleted it will NOT print on the listview */
                currentItem.setDate(currentSb.toString());
            }
        }
        else if ("pubDate".equals(qName)) {         
            parsingPubDate = false;         
            // Set item's title when we parse item->title tag not the channel title tag
            if (currentItem != null) {
                // Set item's title here
                /* if this is deleted it will NOT print on the listview */
                currentItem.setDate(currentSb.toString());
            }
        }
        else if ("link".equals(qName)) {
            parsingLink = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

        dbcurrentItem = new DBItem();

        String strCharacters = new String(ch,start,length);

        if (parsingTitle) {
            if (currentItem != null) {
                // Here we append the title to the buffer due to network issues.
                // Sometimes this characters method is called multiple times for a tag contents.
                currentSb.append(new String(ch, start, length));
                Log.v("Reading Title: ", strCharacters.toString());
                dbcurrentItem.setTitleDB(strCharacters.toString());
                dbConnector.insertTitle(strCharacters.toString());
            }
        }
        else if (parsingDescription) {
            if (currentItem != null) {
                // Here we append the title to the buffer due to network issues.
                // Sometimes this characters method is called multiple times for a tag contents.
                //parse description for any image or video links

                /* Here you need to remove unwanted tages */
                currentSb.append(new String(ch, start, length));
                Log.d("Reading Description: ", strCharacters.toString());
            }
        }
        else if (parsingPubDate) {
            if (currentItem != null) {
                // Here we append the title to the buffer due to network issues.
                // Sometimes this characters method is called multiple times for a tag contents.
                currentSb.append(new String(ch, start, length));
                Log.i("Reading PubDate: ", strCharacters.toString());
                ///
                dbConnector.insertDate(strCharacters.toString());
            }
        }
        else if (parsingLink) {
            if (currentItem != null) {
                currentItem.setLink(new String(ch, start, length));
                Log.i("Reading PostURL: ", strCharacters.toString());
                parsingLink = false;
            }

            //dbConnector.insertTitleDate(dbcurrentItem.getIDDB(),
                 //dbcurrentItem.getTitleDB().toString(),
                 //dbcurrentItem.getPubDateDB().toString());
        }
    }   
}

LogCat

05-31 10:25:42.366: E/AndroidRuntime(5463): FATAL EXCEPTION: AsyncTask #1
05-31 10:25:42.366: E/AndroidRuntime(5463): java.lang.RuntimeException: An error occured while executing doInBackground()
05-31 10:25:42.366: E/AndroidRuntime(5463):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at java.lang.Thread.run(Thread.java:856)
05-31 10:25:42.366: E/AndroidRuntime(5463): Caused by: java.lang.NullPointerException: println needs a message
05-31 10:25:42.366: E/AndroidRuntime(5463):     at android.util.Log.println_native(Native Method)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at android.util.Log.e(Log.java:231)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at com.itcuties.multicategoryrssreader.RssChannelActivity$GetRSSDataTask.doInBackground(RssChannelActivity.java:90)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at com.itcuties.multicategoryrssreader.RssChannelActivity$GetRSSDataTask.doInBackground(RssChannelActivity.java:1)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-31 10:25:42.366: E/AndroidRuntime(5463):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-31 10:25:42.366: E/AndroidRuntime(5463):     ... 4 more
4

2 回答 2

0

I've looked at a few other answers from questions with crashes like yours. Your crash is likely from your error handling here:

Log.e("RssChannelActivity", e.getMessage());

if e.getMessage() is null, then Log.e will crash with an error like described above.

You can use a check like this:

String err = (e.getMessage()==null)?"Error in urls":e.getMessage();
Log.e("RssChannelActivity",err);  

And I still suspect that your root error is connected to this code which may be causing an exception: // Create RSS reader rssReader = new RssReader(urls[0]); saveContact(); // Parse RSS, get items return rssReader.getItems();

Which will give you an exception if urls is null or has 0 objects in it.

于 2013-05-31T03:08:28.583 回答
0

I'll post the answer here so someone else may benefit from it

It's actually quite simple to do what I was asking for.

Here is an example:

// Table name
public static final String TABLE = "feeds";

// Columns
public static final String TIME = "time";
public static final String TITLE = "title";

@Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TABLE + "( " + BaseColumns._ID
                + " integer primary key autoincrement, " + TIME + " integer, "
                + TITLE + " text not null);";
        Log.d("EventsData", "onCreate: " + sql);
        db.execSQL(sql);
    }

And in your Activity which reads the rss feeds (i.e. listview) just add the following line:

ContentValues  values = new ContentValues();    
values.put(DBLHelper.TIME, myRssFeed.getList().get(position).getPubdate());
values.put(DBLHelper.TITLE, myRssFeed.getList().get(position).getTitle());
db.insert(DBLHelper.TABLE, null, values);

That's how simple it is!!!!!

于 2013-06-13T14:54:06.377 回答