Converting single-thread app to ASyncTask app
<p>I am creating a simple RSS reader which displays headlines in ListView, downloading it from the .xml file of specified website. </p>

<p>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.</p>

<p>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. </p>

<p><strong>SimpleRssReaderActivity.java</strong></p>

<pre><code>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 &amp;&amp; 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);
}


}
</code></pre>

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