Browse Source

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...
master
Defend 8 years ago
parent
commit
b112d62836
  1. 17
      app/app.iml
  2. 7
      app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java
  3. 11
      app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java
  4. 106
      app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java

17
app/app.iml

@ -66,14 +66,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
@ -82,6 +74,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
@ -102,7 +102,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />

7
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() {

11
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);
}
}

106
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();
}
}
}

Loading…
Cancel
Save