import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class getUrlResults {
public static List<String> getSearchResults(String searchText) {
return getWordsList(downloadUrl(searchText), searchText);
}
public static List<String> getWordsList(List<String> linesList, String searchText) {
List<String> resultList = new ArrayList<String>();
String lookForText = "<a class='detailslink' href='Details.aspx?word=";
for (String line : linesList) {
if (line.contains(lookForText)) {
String word = line.substring(lookForText.length() + 2, lookForText.length() + 2 + searchText.length());
if (word.equals("%c3%")) continue;
System.out.println(word);
resultList.add(word);
}
}
return resultList;
}
public static List<String> downloadUrl(String searchWord) {
List<String> lines = new ArrayList<String>();
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
url = new URL("http://www.kryssakuten.se/Search.aspx?stype=ord-som-matchar-monster&sword=" + searchWord);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (UnknownHostException uhe) {
System.err.println("Error: No internet connection.");
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
return lines;
}
}