0

I am now working on making get current order book of crypto currencies.

my codes are as shown below.

    public static void bid_ask () {
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet("https://api.binance.com//api/v1/depth?symbol=ETHUSDT");
    System.out.println("Binance ETHUSDT");

    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            try (InputStream stream = entity.getContent()) {
                BufferedReader reader =
                        new BufferedReader(new InputStreamReader(stream));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

result of this code is as follows..

Binance ETHUSDT

"lastUpdateId":236998360,"bids":[["88.98000000","2.30400000",[]]..........,"asks":[["89.04000000","16.06458000",[]],.......

What I want to make is a kind of price array..

Double Binance[][] = [88.98000000][2.30400000][89.04000000][6.06458000]

How can I extract just Price and Qty from the HTTP/GET response?

user1349407
  • 612
  • 3
  • 13
  • 28

2 Answers2

1

If the response is a JSON, use a parser like Jackson and extract the values. This can be then added to array.

This link will help you.

VarunKrish
  • 179
  • 1
  • 9
0

You will need to parse those values out of the variable line:
There are many ways to do that including using regular expressions or simply using String functions. You'd need to create an ArrayList just before the loop and add each price into it. I highly recommend that you use the Money class from java 9 rather than a double or a float. See -> https://www.baeldung.com/java-money-and-currency

fpezzini
  • 774
  • 1
  • 8
  • 17
  • Then BigDecimal is your next best option to handle the price information -> https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html . float and double will lose precision and it will be an endless headache for you :) – fpezzini Dec 11 '18 at 11:09