Quantcast
Channel: Android RSS Feed - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Android RSS Feed

$
0
0

i am creating an app for my final year project at uni.. i am trying to get an rss feed working.. My issue at the moment is that i created a project (RSSActivity) and got the RSS feeds to work. I then copy and pasted all the files (5 classes and 2 layouts) into the appropriate places. There were a few errors: R cannot be found, but this was easily fixed by changing the package to that of the project (football_app). What i am tring to do is be able to click on a button called 'News' and the news appears.. the problem that i am have is that the when i now click on the 'News' button, it say 'Unfortunately football app has stopped'.. i have tried every single class in the intent, and changed the intent activity name appropriately.. can anyone help me.. code below...

 /////////////// HttpFeedSource.java    package com.julian.football_app;    import java.util.ArrayList;    import java.util.List;    class HttpFeedSource implements FeedSource {        protected static final String URL = "http://www.skysports.com/rss/0,20514,11661,00.xml";        public List<RSSItem> getFeed() {            List<RSSItem> itemList = new ArrayList<RSSItem>();            NewsParser parser = new NewsParser(URL);            parser.parse();            NewsParser.RssFeed feed = parser.getFeed();            for (NewsParser.Item i : feed.getItems()) {                itemList.add(new RSSItem(i.getUrl(), i.getTitle(), i.getDescription(), i.getimageUrl()));            }            return itemList;        }    }    ////////////////////// FeedSource.java        package com.julian.football_app;    import java.util.List;    interface FeedSource {        List<RSSItem> getFeed();}<code> 

/////////////////////////////////////////////////////////////////////////////////////////// //MockFeedSource.java

package com.julian.football_app;

import java.util.ArrayList;import java.util.List;

class MockFeedSource implements FeedSource {

public List<RSSItem> getFeed() {    RSSItem item;    final List<RSSItem> items = new ArrayList<RSSItem>();    item = new RSSItem("Android Workshop er gøy", "http://www.ap.no", " this is the desc", "");    items.add(item);    item = new RSSItem("Android Workshop er gøy 2", "http://www.ap.no", "this is the desc", "");    items.add(item);    item = new RSSItem("Android Workshop er gøy3", "http://www.ap.no", "this is the desc", "");    items.add(item);    return items;}

}

</code>/////////////////////////////////////////////////////////////////////////////////////////// NewsParser.java    package com.julian.football_app;    import org.xml.sax.Attributes;    import org.xml.sax.InputSource;    import org.xml.sax.helpers.DefaultHandler;    import javax.xml.parsers.SAXParser;    import javax.xml.parsers.SAXParserFactory;    import java.io.InputStream;    import java.net.URL;    import java.text.ParseException;    import java.text.SimpleDateFormat;    import java.util.ArrayList;    import java.util.Date;    import java.util.HashMap;    import java.util.Locale;    class NewsParser extends DefaultHandler {        public static SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);        private String urlString;        private RssFeed rssFeed;        private StringBuilder text;        private Item item;        private boolean imgStatus;        public NewsParser(String url) {            this.urlString = url;            this.text = new StringBuilder();        }        public void parse() {            InputStream urlInputStream = null;            SAXParserFactory spf;            SAXParser sp;            try {                URL url = new URL(this.urlString);                urlInputStream = url.openConnection().getInputStream();                InputSource is = new InputSource(urlInputStream);                is.setEncoding("ISO-8859-1");                spf = SAXParserFactory.newInstance();                if (spf != null) {                    sp = spf.newSAXParser();                    sp.parse(is, this);                }            } catch (Exception e) {                throw new RuntimeException(e);            } finally {                try {                    if (urlInputStream != null) urlInputStream.close();                } catch (Exception e) {                    throw new RuntimeException(e);                }            }        }        public RssFeed getFeed() {            return (this.rssFeed);        }        public void startElement(String uri, String localName, String qName,                                 Attributes attributes) {            if (qName.equalsIgnoreCase("channel"))                this.rssFeed = new RssFeed();            else if (qName.equalsIgnoreCase("item") && (this.rssFeed != null)) {                this.item = new Item();                this.rssFeed.addItem(this.item);            } else if (qName.equalsIgnoreCase("image") && (this.rssFeed != null))                this.imgStatus = true;        }        public void endElement(String uri, String localName, String qName) {            if (this.rssFeed == null)                return;            if (qName.equalsIgnoreCase("item"))                this.item = null;            else if (qName.equalsIgnoreCase("image"))                this.imgStatus = false;            else if (qName.equalsIgnoreCase("title")) {                if (this.item != null) this.item.title = this.text.toString().trim();                else if (this.imgStatus) this.rssFeed.imageTitle = this.text.toString().trim();                else this.rssFeed.title = this.text.toString().trim();            } else if (qName.equalsIgnoreCase("link")) {                if (this.item != null) this.item.link = this.text.toString().trim();                else if (this.imgStatus) this.rssFeed.imageLink = this.text.toString().trim();                else this.rssFeed.link = this.text.toString().trim();            } else if (qName.equalsIgnoreCase("description")) {                if (this.item != null) this.item.description = this.text.toString().trim();                else this.rssFeed.description = this.text.toString().trim();            } else if (qName.equalsIgnoreCase("url") && this.imgStatus)                this.rssFeed.imageUrl = this.text.toString().trim();            else if (qName.equalsIgnoreCase("language"))                this.rssFeed.language = this.text.toString().trim();            else if (qName.equalsIgnoreCase("generator"))                this.rssFeed.generator = this.text.toString().trim();            else if (qName.equalsIgnoreCase("copyright"))                this.rssFeed.copyright = this.text.toString().trim();            else if (qName.equalsIgnoreCase("pubDate") && (this.item != null)) {                try {                    this.item.pubDate = sdf.parse(this.text.toString().trim());                } catch (ParseException e) {                    throw new RuntimeException();                }            } else if (qName.equalsIgnoreCase("category") && (this.item != null))                this.rssFeed.addItem(this.text.toString().trim(), this.item);            this.text.setLength(0);        }        public void characters(char[] ch, int start, int length) {            this.text.append(ch, start, length);        }        public static class RssFeed {            public String title;            public String description;            public String link;            public String language;            public String generator;            public String copyright;            public String imageUrl;            public String imageTitle;            public String imageLink;            private ArrayList<Item> items;            private HashMap<String, ArrayList<Item>> category;            public void addItem(Item item) {                if (this.items == null)                    this.items = new ArrayList<Item>();                this.items.add(item);            }            public void addItem(String category, Item item) {                if (this.category == null)                    this.category = new HashMap<String, ArrayList<Item>>();                if (!this.category.containsKey(category))                    this.category.put(category, new ArrayList<Item>());                this.category.get(category).add(item);            }            public ArrayList<Item> getItems() {                return items;            }        }        public static class Item implements Comparable<Item> {            public String title;            public String description;            public String link;            public Date pubDate;            private String url;            private String imageUrl;            public String toString() {                return (this.title +": ");            }            public int compareTo(Item o) {                return (int) (o.pubDate.getTime() - pubDate.getTime());            }            public Date getPubDate() {                return pubDate;            }            public String getDescription() {                return description;            }            public String getTitle() {                return title;            }            public String getUrl() {                return url;            }            public String getimageUrl() {                return imageUrl;            }        }    }package com.julian.football_app;import com.julian.football_app.R;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;public class RSSActivity extends Activity {    /**     * Called when the activity is first created.     */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main2);        ListView rssItemList = (ListView) findViewById(R.id.rssListview);        FeedSource feedSource = new HttpFeedSource();        RSSItemAdapter adapter = new RSSItemAdapter(this, R.layout.rssitem, feedSource.getFeed());        rssItemList.setAdapter(adapter);    }}/////////RssItem    package com.julian.football_app;    import java.util.Date;    class RSSItem {        private String url;        private String title;        private String description;        private String imageUrl;        private Date pubDate;        public RSSItem() {        }        public RSSItem(String url, String title, String description, String imageUrl) {            this.url = url;            this.title = title;            this.description = description;            this.imageUrl = imageUrl;        }        public String getUrl() {            return url;        }        public void setUrl(String url) {            this.url = url;        }        public String getTitle() {            return title;        }        public void setTitle(String title) {            this.title = title;        }        public String getDescription() {            return description;        }        public void setDesc(String description) {            this.description = description;        }        public void setImageUrl(String imageUrl) {            this.imageUrl = imageUrl;        }        public String getimageUrl() {            return imageUrl;        }        public void setpubDate(Date pubDate){            this.pubDate = pubDate;        }        public Date getpubDate(){            return pubDate;        }    }////////////////// RssItemAdaptor    package com.julian.football_app;    import android.content.Context;    import android.view.LayoutInflater;    import android.view.View;    import android.view.ViewGroup;    import android.widget.ArrayAdapter;    import android.widget.TextView;    import java.util.List;    import com.julian.football_app.R;    class RSSItemAdapter extends ArrayAdapter<RSSItem> {        private final Context context;        public RSSItemAdapter(Context context, int textViewResourceId,                List<RSSItem> items) {            super(context, textViewResourceId, items);            this.context = context;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            View v = convertView;            if (v == null) {                LayoutInflater vi = (LayoutInflater) context                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);                v = vi.inflate(R.layout.rssitem, null);            }            final RSSItem item = getItem(position);            TextView title = (TextView) v.findViewById(R.id.title);            TextView desc = (TextView) v.findViewById(R.id.description);            TextView url = (TextView) v.findViewById(R.id.url);            // this is what is viewed            title.setText(item.getTitle());            desc.setText(item.getDescription());            url.setText(item.getimageUrl());            return v;        }    }///This is the button i want to display the Rss Feedpackage com.julian.football_app;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;    public class Menu extends Activity {        @Override        protected void onCreate(Bundle savedInstanceState) {            // TODO Auto-generated method stub            super.onCreate(savedInstanceState);            setContentView(R.layout.menu);            //setting the buttons            Button btnews1 = (Button) findViewById(R.id.news_1);            btnews1.setOnClickListener(new View.OnClickListener() {                public void onClick(View v) {                    // TODO Auto-generated method stub                    Intent news1intent = new Intent(Menu.this, news_1.class);                    Menu.this.startActivity(news1intent);                   }            });

Thank you for looking at my code... It would be much appreciated if someone could help me :) (i know its says news_1.class in the intent.. its only cuz i tried everything and went back to original state..)

These are the errors that appear in the LogCat:

03-23 13:54:16.118: E/AndroidRuntime(553): FATAL EXCEPTION: main03-23 13:54:16.118: E/AndroidRuntime(553):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.app.Activity.startActivityForResult(Activity.java:3190)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.app.Activity.startActivity(Activity.java:3297)03-23 13:54:16.118: E/AndroidRuntime(553):  at com.julian.football_app.Menu$1.onClick(Menu.java:24)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.view.View.performClick(View.java:3460)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.view.View$PerformClick.run(View.java:13955)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.os.Handler.handleCallback(Handler.java:605)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.os.Handler.dispatchMessage(Handler.java:92)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.os.Looper.loop(Looper.java:137)03-23 13:54:16.118: E/AndroidRuntime(553):  at android.app.ActivityThread.main(ActivityThread.java:4340)03-23 13:54:16.118: E/AndroidRuntime(553):  at java.lang.reflect.Method.invokeNative(Native Method)03-23 13:54:16.118: E/AndroidRuntime(553):  at java.lang.reflect.Method.invoke(Method.java:511)03-23 13:54:16.118: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)03-23 13:54:16.118: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)03-23 13:54:16.118: E/AndroidRuntime(553):  at dalvik.system.NativeStart.main(Native Method)

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images