-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchData.java
65 lines (54 loc) · 1.65 KB
/
FetchData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.example.fda;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class FetchData extends Thread {
public String url;
String data = "Empty";
public FetchData(String url) {
this.url = url;
}
@Override
public void run() {
try {
URL url = new URL(this.url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String result_line;
while ((result_line = bufferedReader.readLine()) != null) {
result.append(result_line);
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
setData(result.toString());
} catch (IOException e) {
setData(e.toString());
}
}
public boolean startFetch() {
FetchData.this.start();
return true;
}
public boolean onComplete() {
while (true) {
if (!this.isAlive()) {
return true;
}
}
}
public String getResult() {
return this.getData();
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return data;
}
}