package com.ivalentin.gm; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * Class that fetches an online page. * * @author Inigo Valentin * */ public class FetchURL { //List of strings that will contain the output, line by line private List output; private String url; /** * Constructor. */ public FetchURL(){ output = new ArrayList<>(); } /** * Returns the content of the fetched page. * * @return A List of strings containing the lines of the web page. */ public List getOutput(){ return output; } /** * Actually fetches the web page. * * @param u The URL to fetch. */ public void Run(String u){ url = u; Thread t = new Thread() { public void run() { URL textUrl; try { textUrl = new URL(url); BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream())); String StringBuffer; List lines = new ArrayList<>(); while ((StringBuffer = bufferReader.readLine()) != null) { lines.add(StringBuffer); } bufferReader.close(); output = lines; } catch (Exception e) { e.printStackTrace(); output.add(e.toString()); } } }; t.start(); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }