diff options
author | Halzyn | 2015-05-19 15:31:39 +0200 |
---|---|---|
committer | Halzyn | 2015-05-19 15:31:39 +0200 |
commit | 986b24979d2cd002561853a9d3f9af8aa181b969 (patch) | |
tree | 3963a943ef0386290f083304e9210ec3f1d780f2 /app/src/main/java/org | |
parent | basic Filter & Keyword classes (diff) |
Updated RSS package
Implemented XML parsing, general fixes.
Diffstat (limited to 'app/src/main/java/org')
-rw-r--r-- | app/src/main/java/org/rssin/rss/Feed.java | 80 | ||||
-rw-r--r-- | app/src/main/java/org/rssin/rss/FeedItem.java | 77 | ||||
-rw-r--r-- | app/src/main/java/org/rssin/rss/FeedLoader.java | 104 |
3 files changed, 242 insertions, 19 deletions
diff --git a/app/src/main/java/org/rssin/rss/Feed.java b/app/src/main/java/org/rssin/rss/Feed.java index 5d65991..cda5bf6 100644 --- a/app/src/main/java/org/rssin/rss/Feed.java +++ b/app/src/main/java/org/rssin/rss/Feed.java @@ -1,7 +1,87 @@ package org.rssin.rss; +import android.widget.EditText; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + + /** * Created by Randy on 19-5-2015. */ public class Feed { + + private String guid; + private Date pubDate; + private String title; + private String description; + private String link; + private String author; + private String category; + 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, 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 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..5484b5a 100644 --- a/app/src/main/java/org/rssin/rss/FeedItem.java +++ b/app/src/main/java/org/rssin/rss/FeedItem.java @@ -1,6 +1,5 @@ package org.rssin.rss; -import java.net.URL; import java.util.Date; /** @@ -12,26 +11,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 String category; + 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, 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; + this.setGuid(guid); + this.setPubDate(pubDate); + this.setTitle(title); + this.setDescription(description); + this.setLink(link); + this.setAuthor(author); + this.setCategory(category); + this.setComments(comments); + this.setEnclosure(enclosure); + this.setSource(source); } public String getGuid() { @@ -50,7 +49,7 @@ public class FeedItem { return description; } - public URL getLink() { + public String getLink() { return link; } @@ -58,11 +57,11 @@ public class FeedItem { return author; } - public String[] getCategory() { + public String getCategory() { return category; } - public URL getComments() { + public String getComments() { return comments; } @@ -73,4 +72,44 @@ public class FeedItem { public String getSource() { return source; } + + public void setGuid(String guid) { + this.guid = guid; + } + + public void setPubDate(Date pubDate) { + this.pubDate = pubDate; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setLink(String link) { + this.link = link; + } + + public void setAuthor(String author) { + this.author = author; + } + + public void setCategory(String category) { + this.category = category; + } + + public void setComments(String comments) { + this.comments = comments; + } + + public void setEnclosure(String enclosure) { + this.enclosure = enclosure; + } + + public void setSource(String source) { + this.source = source; + } } diff --git a/app/src/main/java/org/rssin/rss/FeedLoader.java b/app/src/main/java/org/rssin/rss/FeedLoader.java index c69cb4c..c26603d 100644 --- a/app/src/main/java/org/rssin/rss/FeedLoader.java +++ b/app/src/main/java/org/rssin/rss/FeedLoader.java @@ -1,7 +1,111 @@ package org.rssin.rss; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Date; + +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, null, 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(); + } } |