- Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathJavaApiStreaming.java
More file actions
Latest commit
67 lines (52 loc) · 2.49 KB
/
Copy pathJavaApiStreaming.java
File metadata and controls
67 lines (52 loc) · 2.49 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
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.util.Date;
importorg.apache.http.*;
importorg.apache.http.client.methods.*;
importorg.apache.http.impl.client.BasicResponseHandler;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.message.BasicHeader;
importorg.apache.http.util.EntityUtils;
importorg.json.simple.JSONObject;
importorg.json.simple.JSONValue;
publicclassJavaApiStreaming {
publicstaticvoidmain (String[]args) throwsIOException {
DefaultHttpClienthttpClient = newDefaultHttpClient();
try {
HttpUriRequesthttpGet = newHttpGet("https://stream-fxpractice.oanda.com/v1/quote?accountId=<your-account-id>&instruments=EUR_USD,USD_CAD,USD_JPY");
httpGet.setHeader(newBasicHeader("Authorization", "Bearer <your-access-token>"));
System.out.println("Executing request: " + httpGet.getRequestLine());
HttpResponseresp = httpClient.execute(httpGet);
HttpEntityentity = resp.getEntity();
if (resp.getStatusLine().getStatusCode() == 200 && entity != null) {
InputStreamstream = entity.getContent();
Stringline;
BufferedReaderbr = newBufferedReader(newInputStreamReader(stream));
while ((line = br.readLine()) != null) {
Objectobj = JSONValue.parse(line);
JSONObjecttick = (JSONObject) obj;
//ignore heartbeats
if (tick.containsKey("instrument")) {
System.out.println("-------");
Stringinstrument = tick.get("instrument").toString();
Stringtime = tick.get("time").toString();
doublebid = Double.parseDouble(tick.get("bid").toString());
doubleask = Double.parseDouble(tick.get("ask").toString());
System.out.println(instrument);
System.out.println(time);
System.out.println(bid);
System.out.println(ask);
}
}
} else {
// print error message
StringresponseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
}
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}