Last active
March 15, 2018 14:20
-
-
Save zzuummaa/6c0f38561c4dc8dbb04adcf9376ece2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Not empty |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.net.ssl.HttpsURLConnection; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
public class main { | |
/** | |
* Доп задание: | |
* 1. Вынести функционал по получению данных от API в отдельный класс | |
* 2. Парсить json библиотекой Jackson (подключить с помощью Maven или Gradle) | |
*/ | |
public static void main(String[] args) { | |
//Я просил за прошлый день данные а не за текущий | |
//https://api.bitfinex.com/v2/<endpoint>/?parameter=value | |
String site = "https://api.bitfinex.com/v2/candles/trade:1D:tBTCUSD/last"; | |
HttpsURLConnection connection = null; | |
try{ | |
connection = (HttpsURLConnection) new URL(site).openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.setUseCaches(false); | |
connection.setConnectTimeout(250); | |
connection.setReadTimeout(2000); | |
connection.connect(); | |
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
String buff; | |
buff = in.readLine(); | |
char [] temp = buff.toCharArray(); | |
System.out.println(buff); | |
int k=0; | |
int i=0; | |
//Не стоит парсить json руками. Для этого есть библиотеки. | |
String low="",high=""; | |
do { | |
//Конкатенация строк- очень плохая идея. Приводит к созданию кучи объектов. | |
//Вместо класса String в таких случаях используется StringBuilder | |
if (k == 3) high += temp[i]; | |
else if (k == 4) low += temp[i]; | |
i++; | |
if (temp[i] == ',') | |
k++; | |
} | |
while (k!=5); | |
low = low.substring(1); | |
high= high.substring(1); | |
System.out.println(low + " "+high ); | |
double dLow = Double.parseDouble(low); | |
double dHigh = Double.parseDouble(high); | |
double medium = (dHigh + dLow) / 2; | |
System.out.println(medium); | |
}catch (Exception e){ | |
//<code>System.out.println(e)</code> - лишний. | |
//<code>e.printStackTrace();</code> уже выводит всю информацию в консоль. | |
System.out.println(e); | |
e.printStackTrace(); | |
}finally { | |
if (connection!=null) connection.disconnect(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment