diff --git a/src/getUrlResults.java b/src/getUrlResults.java new file mode 100644 index 0000000..53cf29c --- /dev/null +++ b/src/getUrlResults.java @@ -0,0 +1,66 @@ +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.util.ArrayList; +import java.util.List; + +public class getUrlResults { + public static List getSearchResults(String searchText) { + return getWordsList(downloadUrl(searchText)); + } + + public static List getWordsList(List linesList) { + List resultList = new ArrayList(); + + boolean passedLine = false; + for (String line : linesList) { + if (line.contains("Visa mer information om ")) { + passedLine = true; + } + if (passedLine) { + System.out.println(line); + resultList.add(line); + } + } + + return resultList; + } + + public static List downloadUrl(String searchWord) { + List lines = new ArrayList(); + + 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); + System.out.println(line); + } + + + } catch (MalformedURLException mue) { + mue.printStackTrace(); + } catch (IOException ioe) { + ioe.printStackTrace(); + } finally { + try { + if (is != null) is.close(); + } catch (IOException ioe) { + // nothing to see here + } + } + + return lines; + } +} \ No newline at end of file