From 6e124073501dcdf17f0e5362c11e22b3c67c77b7 Mon Sep 17 00:00:00 2001 From: Defend Date: Wed, 15 Feb 2017 16:05:19 +0300 Subject: [PATCH] Finally I got really working news feed. Now we can see only one part of it, but this part working perfectly. --- .../defdevteam/tstu/CabinetActivity.java | 8 -- .../defdevteam/tstu/FeedController.java | 20 ---- .../ru/defend/defdevteam/tstu/FeedParser.java | 101 ++++++++---------- .../ru/defend/defdevteam/tstu/FeedReader.java | 5 - .../defend/defdevteam/tstu/MainActivity.java | 1 - 5 files changed, 47 insertions(+), 88 deletions(-) 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 dc9f686..406465e 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java @@ -5,13 +5,11 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.media.Image; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; -import android.support.v7.widget.LinearLayoutCompat; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -29,8 +27,6 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.Chronometer; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; @@ -38,11 +34,7 @@ import android.widget.RelativeLayout; import android.widget.TextView; import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class CabinetActivity extends AppCompatActivity 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 edea727..c815167 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java @@ -1,6 +1,5 @@ package ru.defend.defdevteam.tstu; -import android.util.Log; /** * Created by thedefend on 29.11.16. @@ -49,15 +48,6 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par throw new Exception("Failed to get rss source string from FeedReader class"); } } -// -// public void feedUpdateAction(final String result) { -// activity.runOnUiThread(new Runnable() { -// @Override -// public void run() { -// //activity.newsTextView.setText(result); -// } -// }); -// } private class FeedUpdater extends Thread { @Override @@ -113,16 +103,6 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par } } - public void logIt(final String LOGIT) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - Log.i("MAIN DEBUG MESSAGE:", LOGIT); - } - }); - } - - @Override public void onSuccess(final String result){ FeedParser parser = new FeedParser(result); 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 fb1f7b2..8a0bed8 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java @@ -1,19 +1,12 @@ 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; @@ -59,79 +52,79 @@ public class FeedParser { } + public String deleteHtmlTag(String Line, String a, String b){ + while(true) { + if (Line.contains(a) && Line.contains(b)) { + Line = Line.substring(0, Line.indexOf(a)) + + Line.substring(Line.indexOf(b) + 1, Line.length()); + } else { + break; + } + } + return Line; + } + + 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; + final String title = "title"; + final String description = "description"; + final String link = "link"; + final String date = "date"; + String titleStr = ""; + String descriptionStr = ""; + String linkStr = ""; + String dateStr = ""; + String currentItem = ""; + + void refreshStrings() { + this.titleStr = ""; + this.descriptionStr = ""; + this.linkStr = ""; + this.dateStr = ""; + } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + currentItem = qName.toLowerCase(); 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; + switch (currentItem) { + case title: + titleStr = titleStr.concat(new String(ch, start, length)); + break; + case description: + descriptionStr = descriptionStr.concat(new String(ch, start, length)); + break; + case link: + linkStr = linkStr.concat(new String(ch, start, length)); + break; + case date: + dateStr = dateStr.concat(new String(ch, start, length)); + break; } } } @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) { + if (qName.equalsIgnoreCase("item") && item) { item = false; + descriptionStr = deleteHtmlTag(descriptionStr, "<", ">"); callback.contentDispatcher(titleStr, descriptionStr, ""); + refreshStrings(); } } @Override public void endDocument() throws SAXException { - callback.onSuccessParsing(); + callback.onSuccessParsing(); } } diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java index b26e0fe..e3d428c 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java @@ -1,7 +1,5 @@ package ru.defend.defdevteam.tstu; -import android.os.Handler; - import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; @@ -14,7 +12,6 @@ class FeedReader extends Thread { private String link; private String result; - private FeedController feedController; public enum States { THREAD_NULL, THREAD_WORK, @@ -26,7 +23,6 @@ class FeedReader extends Thread { FeedReader (FeedController feedController, String link) { this.link = link; - this.feedController = feedController; this.threadState = States.THREAD_NULL; } @@ -40,7 +36,6 @@ class FeedReader extends Thread { this.result = getRss(link); this.threadState = States.THREAD_COMPLETE; callback.onSuccess(result); - feedController.logIt("onSuccess called"); } catch (Exception e) { this.threadState = States.THREAD_FAIL; e.printStackTrace(); diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/MainActivity.java b/app/src/main/java/ru/defend/defdevteam/tstu/MainActivity.java index 81b998e..1e648c8 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/MainActivity.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/MainActivity.java @@ -8,7 +8,6 @@ import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; -import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private EditText login, password;