-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAPIUser.java
More file actions
94 lines (58 loc) · 1.79 KB
/
Copy pathAPIUser.java
File metadata and controls
94 lines (58 loc) · 1.79 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import com.google.gson.Gson;
public class APIUser {
private final String USER_AGENT = "Safari";
private String jsonString;
private StockData sd = null;
private String ticker;
public APIUser(String ticker) throws Exception {
this.ticker = ticker;
}
public String urlBuilder(String ticker, String flag) {
// TODO Auto-generated method stub
String url = "https://api.iextrading.com/1.0/stock";
String endingCall = "/quote";
String begTicker = "/";
if(flag.equals("quote")) {
endingCall = "/quote";
}
if(flag.equals("1yr")) {
endingCall = "/chart/1y";
}
url = url + begTicker + ticker + endingCall;
return url;
}
// HTTP GET request
public void sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
jsonString = response.toString();
}
public String getJsonString() {
return jsonString;
}
public StockData getStockData() {
return sd;
}
}