You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.8 KiB
75 lines
1.8 KiB
package ru.defend.defdevteam.tstu;
|
|
|
|
import android.os.Handler;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.URL;
|
|
|
|
/**
|
|
* Created by thedefend on 28.11.16.
|
|
*/
|
|
|
|
public class FeedReader extends Thread implements FeedController.FeedCallback {
|
|
|
|
private String link;
|
|
private String result;
|
|
private FeedController feedController;
|
|
public enum States {
|
|
THREAD_NULL,
|
|
THREAD_WORK,
|
|
THREAD_FAIL,
|
|
THREAD_COMPLETE
|
|
}
|
|
private States threadState;
|
|
|
|
FeedReader (FeedController feedController, String link) {
|
|
this.link = link;
|
|
this.feedController = feedController;
|
|
this.threadState = States.THREAD_NULL;
|
|
}
|
|
|
|
|
|
public void run(){
|
|
try {
|
|
this.threadState = States.THREAD_WORK;
|
|
this.result = getRss(link);
|
|
this.threadState = States.THREAD_COMPLETE;
|
|
//TODO: слишком сильная взаимосвязь с управляющим классом
|
|
feedController.feedUpdateAction(result);
|
|
} catch (Exception e) {
|
|
this.threadState = States.THREAD_FAIL;
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
private String getRss(String link) throws Exception {
|
|
String tempString = null;
|
|
String inputString = "";
|
|
URL rssFeed = new URL(link);
|
|
BufferedReader inputStream = new BufferedReader(
|
|
new InputStreamReader(rssFeed.openStream()));
|
|
|
|
do {
|
|
if(tempString != null) {
|
|
inputString += tempString;
|
|
}
|
|
tempString = inputStream.readLine();
|
|
} while (tempString != null);
|
|
|
|
return inputString;
|
|
}
|
|
|
|
public States getThreadState() {
|
|
return this.threadState;
|
|
}
|
|
|
|
public String getResult() {
|
|
return this.result;
|
|
}
|
|
|
|
public void Callback(){
|
|
|
|
}
|
|
}
|
|
|