Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:40:35+00:00 2026-06-06T14:40:35+00:00

I am creating a simple RSS reader which displays headlines in ListView, downloading it

  • 0

I am creating a simple RSS reader which displays headlines in ListView, downloading it from the .xml file of specified website.

I wrote the app and it works on the single thread, but i want to use ASyncTask so that all the downloading happens in the background and the UI don’t hang.

Now, i have never used AsyncTask before, and i googled it but still i’m not sure where to transfer the methods of my code to which ASyncTask methods. Please help me do it.

SimpleRssReaderActivity.java

package mohit.app.rssreader;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SimpleRssReaderActivity extends ListActivity {
    List headlines;
    List links;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

     // Initializing instance variables arrays
        headlines = new ArrayList();
        links = new ArrayList();

        try {
            URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();

                //  get the XML from an input stream
            xpp.setInput(getInputStream(url), "UTF_8");


            boolean insideItem = false;

                // Returns the type of current event: START_TAG, END_TAG, etc..
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) 
            {
                    if (eventType == XmlPullParser.START_TAG) 
                {

                    if (xpp.getName().equalsIgnoreCase("item")) 
                    {
                        insideItem = true;
                    } 
                    else if (xpp.getName().equalsIgnoreCase("title")) 
                    {
                        if (insideItem)
                            headlines.add(xpp.nextText()); //extract the headline
                    } 
                    else if (xpp.getName().equalsIgnoreCase("link")) 
                    {
                        if (insideItem)
                            links.add(xpp.nextText()); //extract the link of article
                    }
                }
                else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
                {
                    insideItem=false;
                }

                eventType = xpp.next(); //move to next element
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Binding data
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, headlines);

        setListAdapter(adapter);



    }

public InputStream getInputStream(URL url) {
   try {
       return url.openConnection().getInputStream();
   } catch (IOException e) {
       return null;
     }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
   Uri uri = Uri.parse((String) links.get(position));
   Intent intent = new Intent(Intent.ACTION_VIEW, uri);
   startActivity(intent);
}


}

SO thats my whole code, tell me which new methods to create and the codes to transfer in that method. THNX!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:40:37+00:00Added an answer on June 6, 2026 at 2:40 pm
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
           InitTask _initTask = new InitTask();
            _initTask.execute( this );
    
    }
    

    some thing like this ……

    /**
         * sub-class of AsyncTask
         */
        protected class InitTask extends AsyncTask<Context, Integer, ArrayList>
        {
            // -- run intensive processes here
            // -- notice that the datatype of the first param in the class definition matches the param passed to this method 
            // -- and that the datatype of the last param in the class definition matches the return type of this method
                    @Override
                    protected String doInBackground( Context... params ) 
                    {
    
                            return inBackground();
                    }
    
                    // -- gets called just before thread begins
                    @Override
                    protected void onPreExecute() 
                    {
                            Log.i( "makemachine", "onPreExecute()" );
                            super.onPreExecute();
    
                    }
    
                    // -- called from the publish progress 
                    // -- notice that the datatype of the second param gets passed to this method
                    @Override
                    protected void onProgressUpdate(Integer... values) 
                    {
                            super.onProgressUpdate(values);
                            Log.i( "makemachine", "onProgressUpdate(): " +  String.valueOf( values[0] ) );
                    }
    
                    // -- called if the cancel button is pressed
                    @Override
                    protected void onCancelled()
                    {
                            super.onCancelled();
                            Log.i( "makemachine", "onCancelled()" );
    
                    }
    
                    // -- called as soon as doInBackground method completes
                    // -- notice that the third param gets passed to this method
                    @Override
                    protected void onPostExecute( ArrayList result ) 
                    {
                            super.onPostExecute(result);
                            Log.i( "makemachine", "onPostExecute(): " + result );
                  // Binding data
                     ArrayAdapter adapter = new ArrayAdapter(this,
                              android.R.layout.simple_list_item_1, result );
    
                       SimpleRssReaderActivity.this.setListAdapter(adapter);
    
                    }
        }    
     private ArrayList inBackground(){
    
    
    // Initializing instance variables arrays
            headlines = new ArrayList();
            links = new ArrayList();
    
            try {
                URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
    
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);
                XmlPullParser xpp = factory.newPullParser();
    
                    //  get the XML from an input stream
                xpp.setInput(getInputStream(url), "UTF_8");
    
    
                boolean insideItem = false;
    
                    // Returns the type of current event: START_TAG, END_TAG, etc..
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) 
                {
                        if (eventType == XmlPullParser.START_TAG) 
                    {
    
                        if (xpp.getName().equalsIgnoreCase("item")) 
                        {
                            insideItem = true;
                        } 
                        else if (xpp.getName().equalsIgnoreCase("title")) 
                        {
                            if (insideItem)
                                headlines.add(xpp.nextText()); //extract the headline
                        } 
                        else if (xpp.getName().equalsIgnoreCase("link")) 
                        {
                            if (insideItem)
                                links.add(xpp.nextText()); //extract the link of article
                        }
                    }
                    else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
                    {
                        insideItem=false;
                    }
    
                    eventType = xpp.next(); //move to next element
                }
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
         return headlines ;
    
    
    }
    

    this example is as per your code but if possible i would like to give some suggestion not must but should use

    1- creating and setting the adapter work should remain in Oncreate just set the empty Array list there and pass that list to Asytask (in constructor ) and fill the data in same and just call in notify data set changed in onPostExecute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a simple RSS application and I am not that good in
i am creating simple call filter application which restrict unwanted calls. i use following
I am creating a simple FeedReader iPhone app where a RSS is feed is
I'm creating a simple BizTalk 2010 process that will convert a 4010 835 file
I am creating simple console application where I need to load RTF file to
I'm looking for help/advice with creating simple JSP website using equivalent of PHP include+switch
Im creating a simple Rails 2.3.8 gem and need a migration file inside it.
i am creating simple dashboard view in which i using createDashboardItem function item has
Adding CSS to RSS is quite simple as shown in here: http://www.petefreitag.com/item/208.cfm Creating RSS
I'm creating simple payment form that allows users to select from several different gateways

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.