diff options
author | Camil Staps | 2015-05-19 16:01:31 +0200 |
---|---|---|
committer | Camil Staps | 2015-05-19 16:01:31 +0200 |
commit | 75c6997e97a71d4ad57af124561428461d3c7f14 (patch) | |
tree | 06ea9c93f04fbcc48cecd84459478bdcf9072b8b /app/src/main/java/org | |
parent | Filters (diff) | |
parent | Simple toString method (diff) |
Merge branch 'master' into app
Conflicts:
app/src/main/java/org/rssin/rss/Feed.java
Diffstat (limited to 'app/src/main/java/org')
-rw-r--r-- | app/src/main/java/org/rssin/rss/Feed.java | 77 | ||||
-rw-r--r-- | app/src/main/java/org/rssin/rss/FeedItem.java | 85 | ||||
-rw-r--r-- | app/src/main/java/org/rssin/rss/FeedLoader.java | 105 |
3 files changed, 249 insertions, 18 deletions
diff --git a/app/src/main/java/org/rssin/rss/Feed.java b/app/src/main/java/org/rssin/rss/Feed.java index fd271d6..20c0782 100644 --- a/app/src/main/java/org/rssin/rss/Feed.java +++ b/app/src/main/java/org/rssin/rss/Feed.java @@ -2,9 +2,86 @@ package org.rssin.rss; import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + /** * Created by Randy on 19-5-2015. */ public class Feed implements Serializable { private static final long serialVersionUID = 2; + + private String guid; + private Date pubDate; + private String title; + private String description; + private String link; + private String author; + private List<String> category = new LinkedList<>(); + private String comments; + private String enclosure; + private String source; + + private List<FeedItem> posts = new ArrayList<>(); + + public Feed (String guid, Date pubDate, String title, String description, String link, + String author, List<String> category, String comments, String enclosure, String source) + { + this.guid = guid; + this.pubDate = pubDate; + this.title = title; + this.description = description; + this.link = link; + this.author = author; + this.category = category; + this.comments = comments; + this.enclosure = enclosure; + this.source = source; + } + + + public List<FeedItem> getPosts() { + return posts; + } + + public String getGuid() { + return guid; + } + + public Date getPubDate() { + return pubDate; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public String getLink() { + return link; + } + + public String getAuthor() { + return author; + } + + public List<String> getCategory() { + return category; + } + + public String getComments() { return comments; } + + public String getEnclosure() { + return enclosure; + } + + public String getSource() { + return source; + } + } diff --git a/app/src/main/java/org/rssin/rss/FeedItem.java b/app/src/main/java/org/rssin/rss/FeedItem.java index 2ddad4f..1c5348e 100644 --- a/app/src/main/java/org/rssin/rss/FeedItem.java +++ b/app/src/main/java/org/rssin/rss/FeedItem.java @@ -1,7 +1,8 @@ package org.rssin.rss; -import java.net.URL; import java.util.Date; +import java.util.LinkedList; +import java.util.List; /** * Created by Randy on 19-5-2015. @@ -12,26 +13,26 @@ public class FeedItem { private Date pubDate; private String title; private String description; - private URL link; + private String link; private String author; - private String[] category; - private URL comments; + private List<String> category = new LinkedList<>(); + private String comments; private String enclosure; private String source; - public FeedItem(String guid, Date pubDate, String title, String description, URL link, - String author, String[] category, URL comments, String enclosure, String source) + public FeedItem(String guid, Date pubDate, String title, String description, String link, + String author, List<String> category, String comments, String enclosure, String source) { - this.guid = guid; - this.pubDate = pubDate; - this.title = title; - this.description = description; - this.link = link; - this.author = author; + this.setGuid(guid); + this.setPubDate(pubDate); + this.setTitle(title); + this.setDescription(description); + this.setLink(link); + this.setAuthor(author); this.category = category; - this.comments = comments; - this.enclosure = enclosure; - this.source = source; + this.setComments(comments); + this.setEnclosure(enclosure); + this.setSource(source); } public String getGuid() { @@ -50,7 +51,7 @@ public class FeedItem { return description; } - public URL getLink() { + public String getLink() { return link; } @@ -58,11 +59,11 @@ public class FeedItem { return author; } - public String[] getCategory() { + public List<String> getCategory() { return category; } - public URL getComments() { + public String getComments() { return comments; } @@ -73,4 +74,52 @@ public class FeedItem { public String getSource() { return source; } + + void setGuid(String guid) { + this.guid = guid; + } + + + void setPubDate(Date pubDate) { + this.pubDate = pubDate; + } + + void setTitle(String title) { + this.title = title; + } + + void setDescription(String description) { + this.description = description; + } + + void setLink(String link) { + this.link = link; + } + + void setAuthor(String author) { + this.author = author; + } + + void setCategory(String category) { + this.category.add(category); + } + + void setComments(String comments) { + this.comments = comments; + } + + void setEnclosure(String enclosure) { + this.enclosure = enclosure; + } + + void setSource(String source) { + this.source = source; + } + + @Override + public String toString() + { + return title + "\n" + description + " - " + author; + } + } diff --git a/app/src/main/java/org/rssin/rss/FeedLoader.java b/app/src/main/java/org/rssin/rss/FeedLoader.java index c69cb4c..f1ab72e 100644 --- a/app/src/main/java/org/rssin/rss/FeedLoader.java +++ b/app/src/main/java/org/rssin/rss/FeedLoader.java @@ -1,7 +1,112 @@ package org.rssin.rss; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Date; +import java.util.LinkedList; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserFactory; + +import android.util.Log; + + /** * Created by Randy on 19-5-2015. */ public class FeedLoader { + + private String urlString = null; + private XmlPullParserFactory xmlFactoryObject; + public volatile boolean parsingComplete = true; + + public FeedLoader(String url){ + this.urlString = url; + } + + public void parseXMLAndStoreIt(XmlPullParser myParser) { + int event; + String text=null; + FeedItem post = null; + try { + event = myParser.getEventType(); + while (event != XmlPullParser.END_DOCUMENT) { + String name=myParser.getName(); + switch (event) { + case XmlPullParser.START_TAG: + post = new FeedItem(null, null, null, null, null, + null, new LinkedList<String>(), null, null, null); + break; + case XmlPullParser.TEXT: + text = myParser.getText(); + break; + case XmlPullParser.END_TAG: + assert post != null; + if(name.equals("guid")) { + + post.setGuid(text); + } + else if(name.equals("pubDate")) { + post.setPubDate(new Date(text)); + } + else if(name.equals("title")) { + post.setTitle(text); + } + else if(name.equals("description")) { + post.setDescription(text); + } + else if(name.equals("link")) { + post.setLink(text); + } + else if(name.equals("author")) { + post.setAuthor(text); + } + else if(name.equals("category")) { + post.setCategory(text); + } + else if(name.equals("comments")) { + post.setComments(text); + } + else if(name.equals("enclosure")) { + post.setEnclosure(text); + } + else if(name.equals("source")) { + post.setSource(text); + } + break; + } + event = myParser.next(); + } + parsingComplete = false; + } catch (Exception e) { + e.printStackTrace(); + } + } + public void fetchXML(){ + Thread thread = new Thread(new Runnable(){ + @Override + public void run() { + try { + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setReadTimeout(10000 /* milliseconds */); + conn.setConnectTimeout(15000 /* milliseconds */); + conn.setRequestMethod("GET"); + conn.setDoInput(true); + // Starts the query + conn.connect(); + InputStream stream = conn.getInputStream(); + xmlFactoryObject = XmlPullParserFactory.newInstance(); + XmlPullParser myparser = xmlFactoryObject.newPullParser(); + myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); + myparser.setInput(stream, null); + parseXMLAndStoreIt(myparser); + stream.close(); + } catch (Exception ignored) { + } + } + }); + thread.start(); + } } |