From b112d62836373fa4f2ca62b0b4fc601ac7641182 Mon Sep 17 00:00:00 2001 From: Defend Date: Wed, 15 Feb 2017 12:14:34 +0300 Subject: [PATCH] Fixed empty link error print stack on DownloadImageTask. Added sender of content for parser. Now every item of news going right to list. I wrote parser using SAX lib and DefaultHandle. Work in progress... --- app/app.iml | 17 ++- .../defdevteam/tstu/CabinetActivity.java | 7 +- .../defdevteam/tstu/FeedController.java | 11 +- .../ru/defend/defdevteam/tstu/FeedParser.java | 106 ++++++++++++++++-- 4 files changed, 114 insertions(+), 27 deletions(-) diff --git a/app/app.iml b/app/app.iml index 8451206..a16956c 100644 --- a/app/app.iml +++ b/app/app.iml @@ -66,14 +66,6 @@ - - - - - - - - @@ -82,6 +74,14 @@ + + + + + + + + @@ -102,7 +102,6 @@ - diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java index e190e81..dc9f686 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java @@ -321,9 +321,10 @@ public class CabinetActivity extends AppCompatActivity TextView textView = (TextView) layout.findViewById(R.id.newsfeed_item_text); textView.setText(text); - - new DownloadImageTask((ImageView) layout.findViewById(R.id.newsfeed_item_image)) - .execute(link); + if(!link.equals("")) { + new DownloadImageTask((ImageView) layout.findViewById(R.id.newsfeed_item_image)) + .execute(link); + } } public void clearNewsFeed() { diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java index 27928c3..edea727 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java @@ -125,9 +125,6 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par @Override public void onSuccess(final String result){ - addNewsList("Callback Reader Test", result, ""); - addNewsList("Multiple Items Test 1", result, ""); - addNewsList("Multiple Items Test 2", result, ""); FeedParser parser = new FeedParser(result); parser.registerCallback(this); parser.parseXML(); @@ -140,8 +137,7 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par @Override public void onSuccessParsing() { - drawNewsList(this.newsItemList); - drawNewsList(this.newsItemList); + drawNewsList(newsItemList); } @Override @@ -149,6 +145,11 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par } + @Override + public void contentDispatcher(String title, String text, String link) { + addNewsList(title, text, link); + } + } diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java index 4f9f068..fb1f7b2 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java @@ -2,12 +2,20 @@ package ru.defend.defdevteam.tstu; import org.w3c.dom.Document; import org.w3c.dom.Node; +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; +import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; /** * Created by thedefend on 29.11.16. @@ -21,6 +29,7 @@ public class FeedParser { interface ParserCallback { void onSuccessParsing(); void onFailureParsing(); + void contentDispatcher(String title, String text, String link); } void registerCallback(ParserCallback callback) { @@ -31,22 +40,99 @@ public class FeedParser { this.inputXML = inputXML; } +// static NewsItemList.Group parsedItem; +// static void getParsedItem(String title, String description, String link, String date) { +// parsedItem = new NewsItemList().new Group(title, description, date); +// this.callback.contentDispatcher(); +// } + void parseXML() { try { - DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - // Создается дерево DOM документа из файла - Document document = documentBuilder.parse(inputXML); - - // Получаем корневой элемент - Node root = document.getDocumentElement(); - String exp = root.getTextContent(); - //group.set("Node",exp,""); - //newsItemList.add(group); + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser parser = factory.newSAXParser(); + ExtendedHandler handler = new ExtendedHandler(); + InputSource inputSource = new InputSource(new StringReader(inputXML)); + parser.parse(inputSource, handler); } catch (Exception e) { e.printStackTrace(); } - callback.onSuccessParsing(); + } + + public class ExtendedHandler extends DefaultHandler { + boolean item = false; + boolean title = false; + boolean description = false; + boolean link = false; + boolean date = false; + String titleStr; + String descriptionStr; + String linkStr; + String dateStr; + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + if (qName.equalsIgnoreCase("item")) { + item = true; + } + if (item) { + String tagName = qName.toLowerCase(); + switch (tagName) { + case "title": title = true; + break; + case "description": description = true; + break; + case "link": link = true; + break; + case "date": date = true; + } + } + } + + @Override + public void characters(char ch[], int start, int length) throws SAXException { + if(item) { + if (title) { + titleStr = new String(ch, start, length); + title = false; + } else if (description) { + descriptionStr = new String(ch, start, length); + description = false; + } else if (link) { + linkStr = new String(ch, start, length); + link = false; + } else if (date) { + dateStr = new String(ch, start, length); + date = false; + } + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if (title) { + title = false; + return; + } else if (description) { + description = false; + return; + } else if (link) { + link = false; + return; + } else if (date) { + date = false; + return; + } + if (item) { + item = false; + callback.contentDispatcher(titleStr, descriptionStr, ""); + } + } + + @Override + public void endDocument() throws SAXException { + callback.onSuccessParsing(); + } } }