بلاگ
Ethereum: Get values of a Java closable response body
Access to Java’s close response institution
Working with the Binance API Websocket rear points, you can get a list of data as part of the answer. However, not all answers are easily accessible from your Java code. In this article, we will investigate how to get values closer to response.
Binance Api Websocket Review
Before we dive into the decision, let’s quickly review the basics of Binance Api Websocket. The BinanceapiwebsocketeClient class provides an interface for sending messages and receiving via the Websocket Protocol. When receiving a message, it contains various fields that can be achieved by the methods presented.
Problem: Data List as a response body
In your case, you are likely to expect a list of data from Binance by response to a candlestick request. However, if your API reply structure does not have such an field, you will encounter “org.binance.client.Exceptions.binanceapiexception.”
We can use the following methods to solve this problem:
۱
۲.
Solution: the value of the reaction housing extraction
Here is an example of the implementation:
`Java
import java.util.arraylist;
import java.util.list;
Public class candlesticks
Private list
Public List
if (datalist == null || datalist.ismpty ()) {
// Manage a case where data is not received or there is no list
return new Arraylist <> ();
}
to return the datalist;
}
a public barrel class of candlesticks {
Private symbol of strings;
Private list
Public candlesticks
this.Symbol = symbol;
This.nalues = values;
}
}
Candlesticks () are in public
// Create a new API customer copy
Binanceapiwebsocketeclient client = …;
try (closed answer = client.getnewestcandlestick ()) {
List
for (candlestick data: Datimalist) {
if (date.getvalues ()! = null &&! Data.getvalues (). ismpty ()) {
// Pull the values from the candlestick data
Symbol = Data.Symbol;
List
// Manage specific fields as needed (eg “open” use a separate method)
Handlecandlestickfields (symbol, values);
}
}
return reply; // Return the original subject of reply
} catch (binanceapiexception e) {
// Sign in and rethink the exception to maintain strength
System.err.println (“Error Data Receipt:” + E.GetMessage ());
quit e;
}
}
Private static Void Handlecandlestickfields (Line Symbol, List
// Manage specific fields as needed (eg “open” use a separate method)
// …
}
}
`
In this decision:
- First of all, we check whether the list of data is “zero” or empty to avoid possible exceptions to the nullity arrow.
- If the data list contains non -zero data, we get the values from each field from each field using a tested and grab block to catch any exceptions, thrown out of the fields that are not available at the object.
- We use a separate method (Handlecandlestickfields) to handle specific fields in the Candlestickdata class.