ticker first approach
This commit is contained in:
parent
a0ca543492
commit
a24b3ee260
|
|
@ -6,6 +6,7 @@ import com.google.gson.JsonObject;
|
|||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
|
|
@ -26,12 +27,14 @@ public class TickerTracker {
|
|||
public static PriceChangeData getPriceChanges(String symbol) throws IOException {
|
||||
// Construct the API request URL for 24h change
|
||||
Request request24h = new Request.Builder()
|
||||
.url(BINANCE_API_BASE_URL + "?symbol=" + symbol)
|
||||
.url(BINANCE_API_BASE_URL + "?symbol=" + symbol.toUpperCase(Locale.ROOT))
|
||||
.build();
|
||||
|
||||
// Send 24h request
|
||||
try (Response response24h = httpClient.newCall(request24h).execute()) {
|
||||
// Parse the JSON response
|
||||
if (!response24h.isSuccessful()) {
|
||||
throw new IOException("Unexpected code " + response24h);
|
||||
}
|
||||
String responseBody24h = response24h.body().string();
|
||||
JsonObject jsonResponse = JsonParser.parseString(responseBody24h).getAsJsonObject();
|
||||
|
||||
|
|
@ -47,11 +50,14 @@ public class TickerTracker {
|
|||
|
||||
// Fetch historical data for 7d and 30d changes
|
||||
Request historicalRequest = new Request.Builder()
|
||||
.url("https://api.binance.com/api/v3/klines?symbol=" + symbol + "&interval=1d&limit=30")
|
||||
.url("https://api.binance.com/api/v3/klines?symbol=" + symbol.toUpperCase(Locale.ROOT) + "&interval=1d&limit=30")
|
||||
.build();
|
||||
|
||||
// Send historical data request
|
||||
try (Response historicalResponse = httpClient.newCall(historicalRequest).execute()) {
|
||||
if (!historicalResponse.isSuccessful()) {
|
||||
throw new IOException("Unexpected code " + historicalResponse);
|
||||
}
|
||||
String historicalResponseBody = historicalResponse.body().string();
|
||||
|
||||
// Calculate 7d and 30d percentage changes
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
|
@ -17,6 +18,8 @@ import com.example.dcav2gui.R;
|
|||
import com.example.dcav2gui.TickerTracker;
|
||||
import com.example.dcav2gui.databinding.FragmentHomeBinding;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class HomeFragment extends Fragment {
|
||||
|
||||
private HomeViewModel homeViewModel;
|
||||
|
|
@ -220,6 +223,7 @@ public class HomeFragment extends Fragment {
|
|||
String base_3 = "@string/base_price_ticker_3";
|
||||
String quote_3 = "@string/quote_price_ticker_3";
|
||||
|
||||
|
||||
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges( base_1 + quote_1);
|
||||
pricePair1.setText(String.format("%.2f", priceData.getCurrentPrice()));
|
||||
pricePair124hPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent24h()));
|
||||
|
|
|
|||
Loading…
Reference in New Issue