I'm currently making an RSS reader application which uses a SAX Parser, based upon this example http://ift.tt/1NodKht. to display items from a feed within a listView. On long-clicking the list items, information such as title and link are stored in an SQLite database and i can display them in a textview for offline reading.
However, when i try to show the description in the same manner, a blank textview is displayed. Could someone tell me the reason for this? The feed which i am trying to parse has been passed through the "fulltextrssfeed" site so that the tag within the source code contains the full article text - http://ift.tt/1IXjM5g.
Is this because the sax parser is unable to read the format, or have i made an error elsewhere?
Thank you in advance!
RssItem.java
public class RssItem {
protected String _id;
public String title;
protected String link;
protected String page;
protected String completeTextLink;
protected String mainBody;
protected String description;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
//Title get/set
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
//Link get/set
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
//CompleteTextLink get/set
public String getCompleteTextLink() {
return completeTextLink;
}
public void setCompleteTextLink(String completeTextLink) {
this.completeTextLink = completeTextLink;
}
//MainBody get/set
public String getMainBody() {
return mainBody;
}
public void setMainBody(String mainBody) {
this.mainBody = mainBody;
}
//Page get/set
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return title;
}}
MainActivity.java
public class MainActivity extends Activity {
private MainActivity local;
private DatabaseHandler db;
//Method to create main application view
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set view
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//**************************create a button to move to the user's saved feeds screen*****************************
Button myFeedsButton = (Button) findViewById(R.id.myFeedsButton);
myFeedsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this, MyFeedsScreen.class));
}
});
//***************************************************************************************************************
//Create new instance of database handler
db = new DatabaseHandler(this);
//set local ref to this activity
local = this;
GetRSSDataTask task = new GetRSSDataTask();
//start download Rss task - execute method calls the
task.execute("http://ift.tt/1IXjM5g");
//debug thread name
Log.d("RssReaderApp", Thread.currentThread().getName());
}
//*******************************************************************************************************************
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem>>
{
@Override
protected List<RssItem> doInBackground(String... urls) {
//debug task thread name
Log.d("RssReaderApp", Thread.currentThread().getName());
try {
//create a new reader
RssReader rssReader = new RssReader(urls[0]);
//Parse RSS, get items
return rssReader.getItems();
} catch (Exception e) {
Log.e("RssReaderApp", e.getMessage());
}
return null;
}//doInBackground
//is invoked on UI thread after background tasks are complete.
// Results of background task are passed here as a parameter
@Override
protected void onPostExecute(List<RssItem> result)
{
//Gets listview from main.xml
final ListView listItems = (ListView) findViewById(R.id.listMainView);
//Creates a new list adapter - displays an array of strings in listview
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local, android.R.layout.simple_list_item_1, result);
//Set list adapter for listView
listItems.setAdapter(adapter);
//OnItemClick listener set to allow user to access content from title
listItems.setOnItemClickListener(new ListListener(result, local));
//*******************************LONG CLICK FUNCTIONALITY******************************************
//Set new long click listener which should allow item to be stored to db
listItems.setLongClickable(true);
listItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
try {
db.open();
RssItem fromList = (RssItem) listItems.getItemAtPosition(position);
RssItem item = new RssItem();
item.title = fromList.title;
item._id = fromList._id;
item.completeTextLink = fromList.completeTextLink;
item.mainBody = fromList.mainBody;
item.link = fromList.link;
item.description = fromList.description;
// item.page = fromList.page;
db.insertRssItem(item);
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
Toast.makeText(getBaseContext(), "Item saved in My Feeds!", Toast.LENGTH_SHORT).show();
return true;
}
});
}//onPostExecute
}//getRssTaskClass}//class
MyFeedsScreen.java - listview with feeds from db
public class MyFeedsScreen extends ListActivity {
DatabaseHandler dbHandler;
SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_feeds_screen);
dbHandler = new DatabaseHandler(MyFeedsScreen.this);
displayList();
}
public void displayList() {
dbHandler.getWritableDatabase();
dbHandler = new DatabaseHandler(this);
final Cursor cursor = dbHandler.getData();
String from[] = new String[]{dbHandler.columnTitle, dbHandler.link, dbHandler.description};
int to[] = new int[]{R.id.textView1};
dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to, 0);
final ListView lv = getListView();
lv.setAdapter(dataAdapter);
//OnItemClick
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Create intent to take user to the details screen
Intent intent = new Intent(MyFeedsScreen.this, DetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("title", cursor.getString(cursor.getColumnIndexOrThrow("title")));
bundle.putString("link", cursor.getString(cursor.getColumnIndexOrThrow("link")));
bundle.putString("description", cursor.getString(cursor.getColumnIndexOrThrow("description")));
intent.putExtras(bundle);
//cursor.getString(cursor.getColumnIndexOrThrow(cursor.getColumnName(1)));
startActivity(intent);
}
});
}}
ParseHandler.java
public class RssParseHandler extends DefaultHandler
{ private List rssItems;
// Used to reference item while parsing
private RssItem currentItem;
// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingLink;
private boolean parsingCompleteTextLink;
private boolean parsingPage;
private boolean parsingMainBody;
private boolean parsing_id;
private boolean parsingDescription;
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();
}
else if ("title".equals(qName)) {
parsingTitle = true;
}
else if ("link".equals(qName)) {
parsingLink = true;
}
else if ("completeTextLink".equals(qName))
{
parsingCompleteTextLink = true;
}
else if ("page".equals(qName)) {
parsingPage = true;
}
else if ("mainBody".equals(qName))
{
parsingMainBody = true;
}
else if ("_id".equals(qName))
{
parsing_id = true;
}
else if ("description".equals(qName))
{
parsingDescription = 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;
}
else if ("link".equals(qName)) {
parsingLink = false;
}
else if ("completeTextLink".equals(qName)) {
parsingCompleteTextLink = false;
}
else if ("page".equals(qName)) {
parsingPage = false;
}
else if ("mainBody".equals(qName))
{
parsingMainBody = false;
}
else if ("_id".equals(qName))
{
parsing_id = false;
}
else if ("description".equals(qName))
{
parsingDescription = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (parsingTitle)
{
if (currentItem != null)
currentItem.setTitle(new String(ch, start, length));
}
else if (parsingLink) {
if (currentItem != null) {
currentItem.setLink(new String(ch, start, length));
parsingLink = false;
}
else if (parsingCompleteTextLink)
{
if(currentItem != null) {
currentItem.setCompleteTextLink(new String(ch, start, length));
parsingCompleteTextLink = false;
}
}
else if (parsingPage)
{
if(currentItem != null){
currentItem.setPage(new String (ch, start, length));
parsingPage = false;
}
}
else if (parsingMainBody)
{
if(currentItem != null)
{
currentItem.setMainBody(new String (ch, start, length));
parsingMainBody = false;
}
}
else if (parsing_id)
{
if(currentItem != null)
{
currentItem.set_id(new String (ch, start, length));
parsing_id = false;
}
}
else if (parsingDescription)
{
if(currentItem != null)
{
currentItem.setDescription(new String (ch, start, length));
parsingDescription = false;
}
}
}
}
}//rssHandlerClass
DetailsActivity.java - link, title, description set to textview
public class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
//Set data in layout
TextView titleTextView = (TextView) findViewById(R.id.textViewDetailsTitle);
TextView linkTextView = (TextView) findViewById(R.id.textViewDetailsLink);
TextView descriptionTextView = (TextView)findViewById(R.id.textViewToShowFullArticle);
//Bundle which maps Strings to various textViews
Bundle bundle = this.getIntent().getExtras();
titleTextView.setText(bundle.getString("title"));
linkTextView.setText(bundle.getString("link"));
descriptionTextView.setText(bundle.getString("description"));
}}
Aucun commentaire:
Enregistrer un commentaire