Skip to content

Instantly share code, notes, and snippets.

@boshng95
Created October 16, 2019 01:29
Show Gist options
  • Save boshng95/d41c46660aae8ad84b2e6e3af526b609 to your computer and use it in GitHub Desktop.
Save boshng95/d41c46660aae8ad84b2e6e3af526b609 to your computer and use it in GitHub Desktop.
private void getStockPricesOnline(final String companyCode){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
String inputLine;
String result;
boolean successful = false;
while(!successful){
try{
String url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol="
+companyCode+
"&apikey=demo&outputsize=compact";
System.out.println(url);
URL api = new URL(url);
HttpURLConnection connection =(HttpURLConnection) api.openConnection();
connection.connect();
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
while((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
reader.close();
streamReader.close();
result = stringBuilder.toString();
JSONObject jsonObject = new JSONObject(result);
} catch (Exception ex){
ex.printStackTrace();
}
}
}
});
thread.start();
try{
thread.join();
}catch (Exception ex){
ex.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment