Try this code:
package com.example.thehindu_topstories;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity
{
NewsList newsList = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
setContentView(sv);
sv.addView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
new FetchFeed().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class FetchFeed extends AsyncTask<Void, Void, Void>
{
protected Void doInBackground(Void... params)
{
try
{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp= spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL SourceUrl = new URL ("http://beta.thehindu.com/news/?service=rss");
NewsXMLHandler myXMLHandler = new NewsXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(SourceUrl.openStream()));
}
catch (ParseException e)
{
System.out.println("XML Parsing Exception="+ e);
}
catch (MalformedURLException e)
{
System.out.println("XML Parsing Exception="+ e);
}
catch (Exception e)
{
System.out.println("XML Parsing Exception="+ e);
}
return null;
/**/
}
protected void onPostExecute(Void param)
{
Button title[];
TextView pubDate[];
newsList = NewsXMLHandler.newsList;
title = new Button[newsList.getTitle().size()];
pubDate = new TextView[newsList.getpubDate().size()];
for (int i = 1; i <= (newsList.getTitle().size()); i++)
{
try{
title[i] = new Button(this);
title[i].setText(i+"."+newsList.getTitle().get(i)+"\n");
title[i].append(Html.fromHtml( String.format("<a href=\"%s\">(..Read more..)</a> ",newsList.getLink().get(i))));
title[i].setMovementMethod(LinkMovementMethod.getInstance());
title[i].setBackgroundColor(Color.LTGRAY);
pubDate[i]= new TextView(this);
pubDate[i].setText(newsList.getpubDate().get(i)+"\n");
pubDate[i].setTextSize(10);
layout.addView(title[i]);
layout.addView(pubDate[i]);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
}
}