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